Created
May 19, 2021 15:26
-
-
Save petrvecera/67f5fd7c6c4862b6e374b6a344ce65bc to your computer and use it in GitHub Desktop.
Simple JS throttle function which ignores additional calls within the given timeframe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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