Skip to content

Instantly share code, notes, and snippets.

@matijs
Created July 10, 2013 06:36
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 matijs/5963911 to your computer and use it in GitHub Desktop.
Save matijs/5963911 to your computer and use it in GitHub Desktop.
setImmedate polyfill based on http://dbaron.org/log/20100309-faster-timeouts
(function() {
var timeouts = [];
var messageName = "zero-timeout-message";
function handleMessage(event) {
if (event.source === window && event.data === messageName) {
event.stopPropagation();
if (timeouts.length > 0) {
var fn = timeouts.shift();
fn();
}
}
}
if (!window.setImmediate) {
window.setImmediate = function(fn) {
timeouts.push(fn);
window.postMessage(messageName, "*");
}
window.addEventListener("message", handleMessage, true);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment