Created
May 18, 2020 18:09
-
-
Save daltonrooney/dad2fc6ceac36266e5efdc1933f69b67 to your computer and use it in GitHub Desktop.
Mini debounce
This file contains hidden or 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
/* 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