Created
July 13, 2012 15:26
-
-
Save pthrasher/3105469 to your computer and use it in GitHub Desktop.
window.nextTick for the browser. ;-)
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
/* | |
* setTimeout(fn, 0) for realz, for modern browsers. | |
* AKA nextTick from node. | |
* Ripped from http://dbaron.org/log/20100309-faster-timeouts | |
* Most browsers don't actually let you specify timeouts of 0, so the below | |
* method is a better way of achieving this. | |
* Philip Thrasher, 2012 | |
*/ | |
(function(){ | |
var timeouts = [], | |
messageName = 'nextTickPlz', | |
_nextTick = function(fn) { | |
timeouts.push(fn); | |
window.postMessage(messageName, "*"); | |
}, | |
_handleMessage(event) { | |
if (event != null && event.source === window && event.data === messageName) { | |
event.stopPropagation(); | |
if (timeouts.length > 0) { | |
var fn = timeouts.shift(); | |
fn(); | |
} | |
} | |
}; | |
window.addEventListener("message", handleMessage, true); | |
window.nextTick = _nextTick; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not run:
you missed the function declaration for
_handleMessage
andhandleMessage
is not defined. Here's the fixed version: