Skip to content

Instantly share code, notes, and snippets.

@nhunzaker
Created June 8, 2012 19:50
Show Gist options
  • Select an option

  • Save nhunzaker/2897807 to your computer and use it in GitHub Desktop.

Select an option

Save nhunzaker/2897807 to your computer and use it in GitHub Desktop.
function queue() {
queue.stack = queue.stack || [];
var push = function(scope, obj) { Array.prototype.push.call(scope, obj); }
if (arguments.length) {
queue.stack.push(arguments);
if (queue.process) return false;
}
queue.process = function(current) {
push(arguments, function() {
if (queue.stack.length) {
queue();
} else {
delete queue.process
}
})
queue.job.apply(this, arguments)
};
queue.process.apply(this, queue.stack.shift())
};
queue.job = function(num, next) {
setTimeout(function() {
console.log(num);
next();
}, 500)
}
queue(1);
queue(2);
queue(3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment