Skip to content

Instantly share code, notes, and snippets.

@psiborg
Created June 9, 2011 17:46
Show Gist options
  • Save psiborg/1017278 to your computer and use it in GitHub Desktop.
Save psiborg/1017278 to your computer and use it in GitHub Desktop.
JS Queue (with support for a delay)
var queue = function () {
this.items = [];
this.timer = null;
};
queue.prototype = {
add: function (item) {
var self = this;
self.items.push(item);
},
flush: function () {
var self = this;
if (self.timer) {
return;
}
while (self.items.length) {
var fn = self.items.shift();
if (fn.toString().indexOf('delay:') !== -1) {
var ms = Number(fn.split(':')[1]);
self.timer = setTimeout(function () {
self.timer = null;
self.flush();
}, ms);
return;
}
fn();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment