Skip to content

Instantly share code, notes, and snippets.

@anaibol
Created October 20, 2016 20:27
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anaibol/b511550e0d6faed05bf777a50ed37e59 to your computer and use it in GitHub Desktop.
Save anaibol/b511550e0d6faed05bf777a50ed37e59 to your computer and use it in GitHub Desktop.
ES6 debounce
export default function debounce(func, wait, immediate) {
let timeout
return function(...args) {
clearTimeout(timeout)
timeout = setTimeout(() => {
timeout = null
if (!immediate) func.apply(this, args)
}, wait)
if (immediate && !timeout) func.apply(this, [...args])
}
}
@anaibol
Copy link
Author

anaibol commented Apr 27, 2023

2023

const debounce = (func, wait, immediate = false) => (...args) =>
  setTimeout(() => {
    !immediate && func(...args)
  }, wait);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment