Skip to content

Instantly share code, notes, and snippets.

@geastwood
Created May 1, 2014 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save geastwood/11459225 to your computer and use it in GitHub Desktop.
Save geastwood/11459225 to your computer and use it in GitHub Desktop.
setTimeout

pattern to replace setInterval with setTimeout

Register a task with setInterval and a interval is not desirable, since the task can take more time than the interval, so the compiler will skip through the current cycle. Replace setInterval with setTimeout with following pattern:

setInterval(function() {
    runSomething();
}, interval);

with

(function loop() {
    runSomething();
    setTimeout(loop, interval);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment