Created
September 27, 2023 17:36
-
-
Save maxam2017/bdb9cee08c27a00f23073586a7016532 to your computer and use it in GitHub Desktop.
Debounce & Throttle
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 debounce(fn, wait) { | |
let timerId; | |
return function debounced(...args) { | |
clearTimeout(timerId); | |
const that = this; | |
timerId = setTimeout(() => fn.apply(that, args), wait); | |
}; | |
} |
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(fn, wait) { | |
let shouldThrottle = false; | |
return function throttled(...args) { | |
if (shouldThrottle) return; | |
shouldThrottle = true; | |
setTimeout(() => { | |
shouldThrottle = false; | |
}, wait); | |
fn.apply(this, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment