Skip to content

Instantly share code, notes, and snippets.

@stuartpb
Created January 26, 2014 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartpb/8630599 to your computer and use it in GitHub Desktop.
Save stuartpb/8630599 to your computer and use it in GitHub Desktop.
Simple JS function for performing functions in a timed queue.
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