tastypie
Version:
Tastypie is a webservice API framework for Node.js based on Django's Tastypie Framework. It provides a convenient, yet powerful and highly customizable, abstraction for creating REST-style interfaces
34 lines (29 loc) • 824 B
JavaScript
var now = require('../time/now');
/**
*/
function throttle(fn, delay){
var context, timeout, result, args,
diff, prevCall = 0;
function delayed(){
prevCall = now();
timeout = null;
result = fn.apply(context, args);
}
function throttled(){
context = this;
args = arguments;
diff = delay - (now() - prevCall);
if (diff <= 0) {
clearTimeout(timeout);
delayed();
} else if (! timeout) {
timeout = setTimeout(delayed, diff);
}
return result;
}
throttled.cancel = function(){
clearTimeout(timeout);
};
return throttled;
}
module.exports = throttle;