Last active
December 18, 2015 20:59
-
-
Save jeffreyiacono/5844008 to your computer and use it in GitHub Desktop.
generic queue that applies the passed operation to each element, separating each call out by the specified throttle
This file contains 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
function Queue(config) { | |
this.throttle = (typeof config['throttle'] !== "undefined") ? config['throttle'] : 2000; | |
this.elements = config['elements']; | |
this.operation = config['operation']; | |
this.interval_id = null; | |
return this; | |
} | |
Queue.prototype.process = function() { | |
var self = this; | |
this.interval_id = window.setInterval(function() { | |
self.operation(self.elements.shift()); | |
if (self.elements.length <= 0) { self.halt(); } | |
}, this.throttle); | |
} | |
Queue.prototype.halt = function() { | |
clearInterval(this.interval_id); | |
this.log("halted interval " + this.interval_id); | |
this.interval_id = null; | |
} | |
Queue.prototype.log = function(msg) { | |
console.log("[" + new Date + "] " + msg); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ex: