Last active
September 30, 2015 09:37
-
-
Save fendent/1760684 to your computer and use it in GitHub Desktop.
real 0ms setTimeout() / setImmediate()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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