Last active
March 6, 2019 17:43
-
-
Save existenzial/de68872c4e1ad3237a697608de7b57ca to your computer and use it in GitHub Desktop.
Debouncing Function - ES2015
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
/** | |
* debounce | |
* @type {Function} | |
* | |
* @param {Function} cb | |
* @param {Number} wait | |
* @param {Boolean} immediate | |
* | |
* @return {Function} debounced function | |
*/ | |
const debounce = (cb, wait, immediate) => { | |
let timeoutId; | |
return (...args) => { | |
const context = this; | |
if (immediate) { | |
cb(); | |
} | |
clearTimeout(timeoutId); | |
timeoutId = setTimeout(() => cb.apply(context, args), wait); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment