How can a function rate-limit its calls? The calls should not be discarded if too frequent, but rather be queued up and spaced out in time, X milliseconds apart. I\'ve looked at
Should be rather simple without a library:
var stack = [],
timer = null;
function process() {
var item = stack.shift();
// process
if (stack.length === 0) {
clearInterval(timer);
timer = null;
}
}
function queue(item) {
stack.push(item);
if (timer === null) {
timer = setInterval(process, 500);
}
}
http://jsfiddle.net/6TPed/4/