Skip to content

Instantly share code, notes, and snippets.

@amackintosh
Created November 26, 2017 00:24
Show Gist options
  • Save amackintosh/ffc13abeb8663449dc862d5ca94e6dfc to your computer and use it in GitHub Desktop.
Save amackintosh/ffc13abeb8663449dc862d5ca94e6dfc to your computer and use it in GitHub Desktop.
Debounce Function
// Remember, debounced function will not run until it stops getting called for `wait` milliseconds
const debounce = (func, wait) => {
let timeout
return (...args) => {
clearTimeout(timeout)
timeout = setTimeout(() => func.apply(this, args), wait)
}
}
const testFunction = value => console.log(`test: ${value}`)
const debouncedTest = debounce(testFunction, 1000)
for (let i = 0; i < 10; i += 1) debouncedTest('hi')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment