Skip to content

JSL.setInterval()

joesimmons edited this page Sep 25, 2013 · 3 revisions

Sets a custom interval; similar to window.setInterval() but uses a special window.setTimeout() method, with drift accommodation, to make sure the function that was passed is always executing at the most accurate time possible.

window.setInterval() sometimes (depending on the browser) will get off-track and execute your code later and later each time.
JSL.setInterval() fixes that by using a tracking system coupled with window.setTimeout() to execute the code earlier or later, to adjust to how off-track it has gotten.
This function is syntactically the same as window.setInterval()

Tests of drifting are available below.
Note: window.setInterval() is inconsistent across browsers; some, it's accurate, some, it's not.
window.setInterval() - Drift Test
JSL.setInterval() - Drift Test

Important: There still exist bugs in this function. It can still be used, just don't expect perfection yet.




Syntax: JSL.setInterval( fn, intervalTime );


Parameters:

  • fn: A function.
  • intervalTime: An integer, in milliseconds. Every amount of this time that goes by, fn will be executed

Example:

// store a count. this will count to 5 to show you how to clear the interval, as well
var count = 0;

// run the specified function every 1,000 milliseconds
var intervalReference = JSL.setInterval(function () {
    count += 1;

    alert(count);

    if (count >= 5) {
        JSL.clearInterval(intervalReference);
    }
}, 1000);
Clone this wiki locally