Skip to content

Instantly share code, notes, and snippets.

@daltonrooney
Created May 18, 2020 18:09
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 daltonrooney/dad2fc6ceac36266e5efdc1933f69b67 to your computer and use it in GitHub Desktop.
Save daltonrooney/dad2fc6ceac36266e5efdc1933f69b67 to your computer and use it in GitHub Desktop.
Mini debounce
/* https://gist.github.com/nmsdvid/8807205#gistcomment-3168449 */
var debounce = (callback, wait = 250) => {
let timer;
let last_call = 0;
return (...args) => {
clearTimeout(timer);
const now = Date.now(), time_from_last_call = now - last_call;
if (time_from_last_call > wait) {
last_call = now;
callback(...args);
}
else {
timer = setTimeout(() => {
last_call = now;
callback(...args);
}, wait);
}
};
}
this.debounce = debounce;
if (typeof module !== 'undefined' && module !== null && module.exports) {
module.exports = debounce;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment