Created
January 19, 2020 02:54
-
-
Save bitfishxyz/4f5077a1c747d28cd7b64fe21cd75d9c 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