Skip to content

Instantly share code, notes, and snippets.

@yushine
Forked from geastwood/setInteral_setTimeout.md
Created October 26, 2016 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yushine/d9ce22b4fb49e4ebbb6f2bdfbe6450e7 to your computer and use it in GitHub Desktop.
Save yushine/d9ce22b4fb49e4ebbb6f2bdfbe6450e7 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