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);
}
}