Created
April 3, 2023 09:09
-
-
Save giacomoferretti/55b21c8d3d6f14836b4aa21b1677e14d to your computer and use it in GitHub Desktop.
JavaScript throttle and debounce arrow functions
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
const throttle = (func, delay) => { | |
let timeoutId; | |
return (...args) => { | |
if (!timeoutId) { | |
timeoutId = setTimeout(() => { | |
func.apply(this, args); | |
timeoutId = null; | |
}, delay); | |
} | |
}; | |
}; | |
const debounce = (func, delay) => { | |
let timeoutId; | |
return (...args) => { | |
clearTimeout(timeoutId); | |
timeoutId = setTimeout(() => { | |
func.apply(this, args); | |
}, delay); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment