Hi,
First of all, sorry for my non-perfect english.
I've made few times ago, using a system of time buffering, a "to my
mind" 100% accurate timer.
And i don't found any info about this approach on Google.
I use something close to this:
var TimeMachine = {
start: function () {
this.currentFrame = 0;
this.isRunning = true;
this.startTime = new Date();
this.looper()
},
stop: function () {
clearTimeout(this.TO);
this.isRunning = false;
},
looper: function () {
var now = new Date;
if (this.isRunning) {
// wait for the difference to be a divisor of the fps (here
25 fps, one frame every 40ms)
while ((now - this.startTime) % 40) != 0) {
now = new Date;
}
console.log((new Date()).getTime()); // should log every 40ms
// and wait 30ms to call the looper again
this.TO = setTimeout(
function (that) {
return function () {
that.looper();
}
}(this), 30);
}
}
};
The looper method freeze the execution of javascript (while) for
approximatly 10ms (40 - 30) but 25 times a second....
It's heavy for the cpu... but it's accurate :-)
And if the timer miss a step because of a function that take more than
30ms to execute, the next step will be in the right "rythm".
I've made some test using Web Worker to improve accuracy ,
but the main thread is flood with 25 message events by seconds, and it's
seems to be a little bit hard to manage.
But i'm pretty sure we can do something nice with this, but for a only
thing in a row...
Hope to have been helpful.
Le 05/04/2011 03:52, Jared Hirsch a écrit :
It's probably only worth it to have the server ping the client if
you're waiting for something to happen on the server. If that's the
case, then take a look at comet approaches. Otherwise, you're probably
not going to do better than running a local timer on the client, IMO.
And you're right--there's no perfect synchronization across any
network. If you're interested in learning more about this problem in
general, check out articles on ntp, the network time protocol.
Jared
On Monday, April 4, 2011, Dmitry Pashkevich<[email protected]> wrote:
Hi Peter!
To my knowledge it's impossible to build a 100% accurate timer in JS since the
language is asynchronous in it's nature: there's no guarantee that an event
scheduled to occur at some point in time would execute exactly at that moment
because there may be other pending items in the event loop.
I suggest you read 2 really good insightful articles from John Resig:
http://ejohn.org/blog/how-javascript-timers-work/
http://ejohn.org/blog/accuracy-of-javascript-time/
They explain why there is a precision issue in javascript timers and important
difference between setTimeout and setInterval functions.
You can, however, achieve some level of precision or at least assess the error
you get by taking the mentioned issues into account. Take a look at javascript
benchmarks like http://jsperf.com/
I hardly doubt that your approach on contacting the server to get the time is
viable because the network latency that will seriously affect your measurings
is completely unpredictable.
Please correct me anyone if I am wrong.
Dmitriy.
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]