Skip to content

Instantly share code, notes, and snippets.

@CoralSilver
Last active December 6, 2019 02:11
Show Gist options
  • Save CoralSilver/c607ca03e52c1b77a4b50f00ce81763c to your computer and use it in GitHub Desktop.
Save CoralSilver/c607ca03e52c1b77a4b50f00ce81763c to your computer and use it in GitHub Desktop.
Debounce function implemented in ES6
// As long as it continues to be invoked, the callback will not be triggered.
// The function will be called after it stops being called for N milliseconds.
const debounce = (callback, wait) => {
// there is no timeout by default
let timeout = null
return function(...args) {
// last timeout is cleared every time after first call
clearTimeout(timeout)
// callback will only be fired if it was not cleared by invocation occuring before N milliseconds
timeout = setTimeout(() => callback.apply(this, args), wait)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment