Last active
December 6, 2019 02:11
-
-
Save CoralSilver/c607ca03e52c1b77a4b50f00ce81763c to your computer and use it in GitHub Desktop.
Debounce function implemented in ES6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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