How to create an accurate timer in javascript? How to create an accurate timer in javascript? javascript javascript

How to create an accurate timer in javascript?


Why is it not accurate?

Because you are using setTimeout() or setInterval(). They cannot be trusted, there are no accuracy guarantees for them. They are allowed to lag arbitrarily, and they do not keep a constant pace but tend to drift (as you have observed).

How can I create an accurate timer?

Use the Date object instead to get the (millisecond-)accurate, current time. Then base your logic on the current time value, instead of counting how often your callback has been executed.

For a simple timer or clock, keep track of the time difference explicitly:

var start = Date.now();setInterval(function() {    var delta = Date.now() - start; // milliseconds elapsed since startoutput(Math.floor(delta / 1000)); // in seconds    // alternatively just show wall clock time:    output(new Date().toUTCString());}, 1000); // update about every second

Now, that has the problem of possibly jumping values. When the interval lags a bit and executes your callback after 990, 1993, 2996, 3999, 5002 milliseconds, you will see the second count 0, 1, 2, 3, 5 (!). So it would be advisable to update more often, like about every 100ms, to avoid such jumps.

However, sometimes you really need a steady interval executing your callbacks without drifting. This requires a bit more advanced strategy (and code), though it pays out well (and registers less timeouts). Those are known as self-adjusting timers. Here the exact delay for each of the repeated timeouts is adapted to the actually elapsed time, compared to the expected intervals:

var interval = 1000; // msvar expected = Date.now() + interval;setTimeout(step, interval);function step() {    var dt = Date.now() - expected; // the drift (positive for overshooting)    if (dt > interval) {        // something really bad happened. Maybe the browser (tab) was inactive?        // possibly special handling to avoid futile "catch up" run    }    … // do what is to be done    expected += interval;    setTimeout(step, Math.max(0, interval - dt)); // take into account drift}


I'ma just build on Bergi's answer (specifically the second part) a little bit because I really liked the way it was done, but I want the option to stop the timer once it starts (like clearInterval() almost). Sooo... I've wrapped it up into a constructor function so we can do 'objecty' things with it.

1. Constructor

Alright, so you copy/paste that...

/** * Self-adjusting interval to account for drifting *  * @param {function} workFunc  Callback containing the work to be done *                             for each interval * @param {int}      interval  Interval speed (in milliseconds) * @param {function} errorFunc (Optional) Callback to run if the drift *                             exceeds interval */function AdjustingInterval(workFunc, interval, errorFunc) {    var that = this;    var expected, timeout;    this.interval = interval;    this.start = function() {        expected = Date.now() + this.interval;        timeout = setTimeout(step, this.interval);    }    this.stop = function() {        clearTimeout(timeout);    }    function step() {        var drift = Date.now() - expected;        if (drift > that.interval) {            // You could have some default stuff here too...            if (errorFunc) errorFunc();        }        workFunc();        expected += that.interval;        timeout = setTimeout(step, Math.max(0, that.interval-drift));    }}

2. Instantiate

Tell it what to do and all that...

// For testing purposes, we'll just increment// this and send it out to the console.var justSomeNumber = 0;// Define the work to be donevar doWork = function() {    console.log(++justSomeNumber);};// Define what to do if something goes wrongvar doError = function() {    console.warn('The drift exceeded the interval.');};// (The third argument is optional)var ticker = new AdjustingInterval(doWork, 1000, doError);

3. Then do... stuff

// You can start or stop your timer at willticker.start();ticker.stop();// You can also change the interval while it's in progressticker.interval = 99;

I mean, it works for me anyway. If there's a better way, lemme know.


Most of the timers in the answers here will linger behind the expected time because they set the "expected" value to the ideal and only account for the delay that the browser introduced before that point. This is fine if you just need accurate intervals, but if you are timing relative to other events then you will (nearly) always have this delay.

To correct it, you can keep track of the drift history and use it to predict future drift. By adding a secondary adjustment with this preemptive correction, the variance in the drift centers around the target time. For example, if you're always getting a drift of 20 to 40ms, this adjustment would shift it to -10 to +10ms around the target time.

Building on Bergi's answer, I've used a rolling median for my prediction algorithm. Taking just 10 samples with this method makes a reasonable difference.

var interval = 200; // msvar expected = Date.now() + interval;var drift_history = [];var drift_history_samples = 10;var drift_correction = 0;function calc_drift(arr){  // Calculate drift correction.  /*  In this example I've used a simple median.  You can use other methods, but it's important not to use an average.   If the user switches tabs and back, an average would put far too much  weight on the outlier.  */  var values = arr.concat(); // copy array so it isn't mutated    values.sort(function(a,b){    return a-b;  });  if(values.length ===0) return 0;  var half = Math.floor(values.length / 2);  if (values.length % 2) return values[half];  var median = (values[half - 1] + values[half]) / 2.0;    return median;}setTimeout(step, interval);function step() {  var dt = Date.now() - expected; // the drift (positive for overshooting)  if (dt > interval) {    // something really bad happened. Maybe the browser (tab) was inactive?    // possibly special handling to avoid futile "catch up" run  }  // do what is to be done         // don't update the history for exceptionally large values  if (dt <= interval) {    // sample drift amount to history after removing current correction    // (add to remove because the correction is applied by subtraction)      drift_history.push(dt + drift_correction);    // predict new drift correction    drift_correction = calc_drift(drift_history);    // cap and refresh samples    if (drift_history.length >= drift_history_samples) {      drift_history.shift();    }      }     expected += interval;  // take into account drift with prediction  setTimeout(step, Math.max(0, interval - dt - drift_correction));}