Last active
December 8, 2020 09:22
-
-
Save sungchuni/1791a0b9c269c8d0caad0b2ea23cd725 to your computer and use it in GitHub Desktop.
simple implementation debounce
This file contains 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
function debounce(fn, wait) { | |
let cancelId = null | |
const hasWait = Number.isSafeInteger(wait) | |
const [timer, cancel] = | |
hasWait | |
? [setTimeout, clearTimeout] | |
: [requestAnimationFrame, cancelAnimationFrame] | |
return function debounced(...args) { | |
cancel(cancelId) | |
cancelId = timer(fn.bind(this, ...args), wait) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment