Skip to content

Instantly share code, notes, and snippets.

@gimmi
Created December 26, 2022 15:54
Show Gist options
  • Save gimmi/9588f8a616a6a4325a7c6b684b2e57c7 to your computer and use it in GitHub Desktop.
Save gimmi/9588f8a616a6a4325a7c6b684b2e57c7 to your computer and use it in GitHub Desktop.
JavaScript
function debounce(func, timeout = 300) {
let timerId = null;
const debouncedFunc = (...args) => {
window.clearTimeout(timerId)
timerId = window.setTimeout(() => func(...args), timeout)
}
debouncedFunc.cancel = () => {
window.clearTimeout(timerId)
}
return debouncedFunc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment