Created
January 26, 2014 09:40
-
-
Save stuartpb/8630599 to your computer and use it in GitHub Desktop.
Simple JS function for performing functions in a timed queue.
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 iq(ms, f, q) { | |
q = q || []; | |
var timer = null; | |
function drain() { | |
var p = q.shift(); | |
if (p) { | |
f ? f(p) : p(); | |
} else { | |
clearInterval(timer); | |
timer = null; | |
} | |
} | |
return function (item) { | |
if (item) q.push(item); | |
if (!timer) { | |
timer = setInterval(drain, ms); | |
drain(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment