Created
October 21, 2022 01:32
-
-
Save MarlonPassos-git/f8b9765d12d934d83a1d3271aa56e4e9 to your computer and use it in GitHub Desktop.
debounce with cancel and return
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 debounce = ( | |
{ delay }, | |
func | |
) => { | |
let timer= null | |
let c = true | |
function debounced (...args) { | |
if (c) { | |
clearTimeout(timer) | |
timer = setTimeout(() =>{ | |
return c ? func(...args) : undefined | |
} , delay) | |
} | |
else { | |
func(...args) | |
} | |
} | |
debounced.cancel = () => { | |
c = false | |
} | |
debounced.volta = () => { | |
c = true | |
} | |
return debounced | |
} | |
const func = () => console.log("adsfasffadsf") | |
const debounceFunc = debounce({ delay: 10000}, func) | |
window.addEventListener("click", debounceFunc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment