Skip to content

Instantly share code, notes, and snippets.

@sungchuni
Last active December 8, 2020 09:22
Show Gist options
  • Save sungchuni/1791a0b9c269c8d0caad0b2ea23cd725 to your computer and use it in GitHub Desktop.
Save sungchuni/1791a0b9c269c8d0caad0b2ea23cd725 to your computer and use it in GitHub Desktop.
simple implementation debounce
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