Skip to content

Instantly share code, notes, and snippets.

@catdad
Last active August 29, 2015 14:06
Show Gist options
  • Save catdad/d56015d3a2bd61781eb2 to your computer and use it in GitHub Desktop.
Save catdad/d56015d3a2bd61781eb2 to your computer and use it in GitHub Desktop.
var Queue = function(){
var deferArray = [],
running = false;
function recursiveExecute(done) {
// maintain scope
(function recurse(){
if (running && deferArray.length) {
var func = deferArray.shift();
// continue on the next event loop iteration
setTimeout(function(){
func(recurse);
}, 0);
} else {
if (done && (typeof done === 'function')) {
done();
}
}
})();
}
this.push = function(func) {
deferArray.push(function(cb){
func();
cb();
});
};
this.run = function(done){
running = true;
recursiveExecute(done);
};
this.stop = function(){
running = false;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment