Skip to content

Instantly share code, notes, and snippets.

@MarlonPassos-git
Created October 21, 2022 01:32
Show Gist options
  • Save MarlonPassos-git/f8b9765d12d934d83a1d3271aa56e4e9 to your computer and use it in GitHub Desktop.
Save MarlonPassos-git/f8b9765d12d934d83a1d3271aa56e4e9 to your computer and use it in GitHub Desktop.
debounce with cancel and return
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