Created
June 9, 2011 17:46
-
-
Save psiborg/1017278 to your computer and use it in GitHub Desktop.
JS Queue (with support for a delay)
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
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