Ajax Throttle
/**
* Throttles a function from performing until after a specified wait period has elapsed.
*
* @param callback Function passed as an argument.
* @param args Parameters passed to callback function.
* @param wait Period of time to wait before performing callback function.
* @returns {Function}
*/
function throttle(callback, args, wait) {
var timeout;
if ( ! timeout) {
setTimeout(function () {
console.log('inside callback timeout');
callback.apply(this, args);
}, wait);
} else {
console.log('problem')
}
}
function RateLimit(fn, delay, context) {
var queue = [], timer = null;
function processQueue() {
var item = queue.shift();
if (item)
fn.apply(item.context, item.arguments);
if (queue.length === 0)
clearInterval(timer), timer = null;
}
return function limited() {
queue.push({
context: context || this,
arguments: [].slice.call(arguments)
});
if (!timer) {
processQueue(); // start immediately on the first invocation
timer = setInterval(processQueue, delay);
}
}
}
function foo(param1, param2) {
var p = document.createElement('p');
p.innerText = param1 + (param2 || '');
document.body.appendChild(p);
}
foo('Starting');
var bar = RateLimit(foo, 2000);
bar(1, 'baz');
bar(2, 'quux');
bar(3);
bar(4, 'optional');
bar(5, 'parameters');
bar(6);
bar(7);
bar(8);
bar(9);