Skip to content

Instantly share code, notes, and snippets.

@dawsontoth
Created October 6, 2011 07:00
Show Gist options
  • Save dawsontoth/1266717 to your computer and use it in GitHub Desktop.
Save dawsontoth/1266717 to your computer and use it in GitHub Desktop.
Deferred Execution Queue
var deferredExecutionQueue = [];
function deferExecution(func, delayMS) {
deferredExecutionQueue.push({ func: func, time: new Date().getTime() + delayMS });
}
setInterval(function() {
if (!deferredExecutionQueue.length)
return;
if (deferredExecutionQueue[0].time < new Date().getTime()) {
var deferred = deferredExecutionQueue.pop();
deferred.func();
deferred.func = deferred.time = null;
}
}, 500);
@dawsontoth
Copy link
Author

Usage:
deferExecution(myFunction, 1000);

Instead of:
setTimeout(myFunction, 1000);
or, the frequently used:
setTimeout(function() { myFunction(); }, 1000);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment