Skip to content

Instantly share code, notes, and snippets.

@imbcmdth
Created May 16, 2013 20:41
Show Gist options
  • Save imbcmdth/5594937 to your computer and use it in GitHub Desktop.
Save imbcmdth/5594937 to your computer and use it in GitHub Desktop.
(function(global){
var exeQueue = [];
exeQueue.insertOrdered = function(val) {
var l = this.length,
order = val.order;
while(l--) {
if(this[l].order > order) break;
}
this.splice(l+1, 0, val);
}
function executeNext() {
var next = exeQueue.pop();
if (next) {
next.callback.apply(this, next.args);
}
}
global.setTimeout = function (fn, delay) {
var runOrder = Date.now() + delay,
args = Array.prototype.slice.call(arguments, 2),
wrappedFn = function () {
fn.apply(this, arguments);
return executeNext();
};
exeQueue.insertOrdered({
order : runOrder,
callback : wrappedFn,
args: args
});
};
global.executeTimeouts = executeNext;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment