Skip to content

Instantly share code, notes, and snippets.

@petrvecera
Created May 19, 2021 15:26
Show Gist options
  • Save petrvecera/67f5fd7c6c4862b6e374b6a344ce65bc to your computer and use it in GitHub Desktop.
Save petrvecera/67f5fd7c6c4862b6e374b6a344ce65bc to your computer and use it in GitHub Desktop.
Simple JS throttle function which ignores additional calls within the given timeframe
function throttle(callback, delay) {
let isThrottled = false;
function wrapper() {
if (isThrottled) {
return;
}
isThrottled = true;
callback.apply(this, arguments);
setTimeout(() => {
isThrottled = false;
}, delay);
}
return wrapper;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment