Skip to content

Instantly share code, notes, and snippets.

@bitfishxyz
Created January 19, 2020 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bitfishxyz/4f5077a1c747d28cd7b64fe21cd75d9c to your computer and use it in GitHub Desktop.
Save bitfishxyz/4f5077a1c747d28cd7b64fe21cd75d9c to your computer and use it in GitHub Desktop.
const debounce = (func, time = 17, options = {
leading: true,
context: null
}) => {
let timer;
const _debounce = function (...args) {
if (timer) {
clearTimeout(timer)
}
if (options.leading && !timer) {
timer = setTimeout(null, time)
func.apply(options.context, args)
}else{
timer = setTimeout(() => {
func.apply(options.context, args)
timer = null
}, time)
}
};
_debounce.cancel = function () {
clearTimeout(timer)
timer = null
};
return _debounce
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment