Skip to content

Instantly share code, notes, and snippets.

@fendent
Last active September 30, 2015 09:37
Show Gist options
  • Save fendent/1760684 to your computer and use it in GitHub Desktop.
Save fendent/1760684 to your computer and use it in GitHub Desktop.
real 0ms setTimeout() / setImmediate()
/**
* fireNext(function() { ... });
*
* Uses postMessage() instead of setTimeout() to achieve a ~0ms timer
* Sean Murphy - https://gist.github.com/1760684
*
* Edit 01/13: Apparently the kids call this setImmediate().
*/
(function(id, q, w) { w.addEventListener('message', function(e) {
id == e.data && e.stopPropagation() && (q.shift() || function() {
throw new Error(id + '(): Expected Function'); })()); } }, true);
w[id] = function(fn) { q.push(fn); w.postMessage(id, w.location);
}; }('fireNext', [], window)); /* Sean - http://im.superla.me/ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment