Skip to content

Instantly share code, notes, and snippets.

@existenzial
Last active March 6, 2019 17:43
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 existenzial/de68872c4e1ad3237a697608de7b57ca to your computer and use it in GitHub Desktop.
Save existenzial/de68872c4e1ad3237a697608de7b57ca to your computer and use it in GitHub Desktop.
Debouncing Function - ES2015
/**
* 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