Skip to content

Instantly share code, notes, and snippets.

@YagamiLight126
Forked from anaibol/debounce.js
Created June 6, 2018 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YagamiLight126/a933daa0722db51e702d1df64c537597 to your computer and use it in GitHub Desktop.
Save YagamiLight126/a933daa0722db51e702d1df64c537597 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])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment