Skip to content

Instantly share code, notes, and snippets.

@r3b
Created February 12, 2014 06:01
Show Gist options
  • Select an option

  • Save r3b/8950719 to your computer and use it in GitHub Desktop.

Select an option

Save r3b/8950719 to your computer and use it in GitHub Desktop.
Turn a constant, erratic method execution into a nice, steady pace.
function defib(fn, space, scope) {
space || (space = 1000);
var queue = [], timer;
function start() {
if (queue.length) {
var exec = queue.shift(),
context = exec.context,
args = exec.args;
fn.apply(context, args);
} else {
console.error("queue empty; clear timer.")
clearInterval(timer);
}
}
return function() {
var context = scope || this,
args = [].concat(Array.prototype.slice.call(arguments));
queue.push({
context: context,
args: arguments
});
if (!timer) timer = setInterval(start, space);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment