Skip to content

Instantly share code, notes, and snippets.

@blooddy
Created January 4, 2017 17:48
Show Gist options
  • Save blooddy/8cb24cf783c4b059ef43b9638c30a1f5 to your computer and use it in GitHub Desktop.
Save blooddy/8cb24cf783c4b059ef43b9638c30a1f5 to your computer and use it in GitHub Desktop.
/** @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