just-throttle
Version:
return a throttled function
67 lines (52 loc) • 1.25 kB
JavaScript
module.exports = throttle;
function throttle(fn, interval, options) {
var timeoutId = null;
var throttledFn = null;
var leading = (options && options.leading);
var trailing = (options && options.trailing);
if (leading == null) {
leading = true; // default
}
if (trailing == null) {
trailing = !leading; //default
}
if (leading == true) {
trailing = false; // forced because there should be invocation per call
}
var cancel = function() {
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}
};
var flush = function() {
var call = throttledFn;
cancel();
if (call) {
call();
}
};
var throttleWrapper = function() {
var callNow = leading && !timeoutId;
var context = this;
var args = arguments;
throttledFn = function() {
return fn.apply(context, args);
};
if (!timeoutId) {
timeoutId = setTimeout(function() {
timeoutId = null;
if (trailing) {
return throttledFn();
}
}, interval);
}
if (callNow) {
callNow = false;
return throttledFn();
}
};
throttleWrapper.cancel = cancel;
throttleWrapper.flush = flush;
return throttleWrapper;
}