Created
July 10, 2013 06:36
-
-
Save matijs/5963911 to your computer and use it in GitHub Desktop.
setImmedate polyfill based on http://dbaron.org/log/20100309-faster-timeouts
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
(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