Created
January 4, 2017 17:48
-
-
Save blooddy/8cb24cf783c4b059ef43b9638c30a1f5 to your computer and use it in GitHub Desktop.
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
/** @type {function((Function|string), ...*):number} */ | |
var setImmediate = window.setImmediate || ( function() { | |
var id = Math.random(); | |
var queue = []; | |
var FunctionCall = Function.prototype.call; | |
window.addEventListener( 'message', function(event) { | |
if ( event.data != id ) return; | |
var q = queue.splice( 0 ); | |
while ( q.length ) { | |
FunctionCall.apply( FunctionCall, q.shift() ); | |
} | |
} ); | |
return function() { | |
if ( !queue.length ) { | |
window.postMessage( id, '*' ); | |
} | |
queue.push( arguments ); | |
}; | |
}() ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment