Skip to content

Instantly share code, notes, and snippets.

@philbirnie
Created June 5, 2016 15:27
Show Gist options
  • Save philbirnie/8ad23dbc3f1be41755432395b7721544 to your computer and use it in GitHub Desktop.
Save philbirnie/8ad23dbc3f1be41755432395b7721544 to your computer and use it in GitHub Desktop.
Throttling Function, based heavily on Underscore's but updated for ES6.
// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time. Normally, the throttled function will run
// as much as it can, without ever going more than once per `wait` duration;
// but if you'd like to disable the execution on the leading edge, pass
// `{leading: false}`. To disable execution on the trailing edge, ditto.
const throttle = function(func, wait, options) {
let timeout, context, args, result;
let previous = 0;
/** There is nothing wrong with doing this **/
/* eslint-disable no-param-reassign*/
if (!options) {
options = {};
}
/* eslint-enable no-param-reassign*/
const later = function() {
if (options.leading === false) {
previous = 0;
} else {
previous = new Date().getTime();
}
timeout = null;
result = Reflect.apply(func, context, args);
if (!timeout) context = args = null;
};
const throttled = function(...eventArguments) {
const now = new Date().getTime();
args = eventArguments;
if (!previous && options.leading === false) previous = now;
const remaining = wait - (now - previous);
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = Reflect.apply(func, this, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
throttled.cancel = function() {
clearTimeout(timeout);
previous = 0;
timeout = context = args = null;
};
return throttled;
};
module.exports = throttle;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment