Last active
August 29, 2015 14:06
-
-
Save catdad/d56015d3a2bd61781eb2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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