-
-
Save jupegarnica/4ac4be2fd14f87edeb1c6b309c55123e to your computer and use it in GitHub Desktop.
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
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