Skip to content

Instantly share code, notes, and snippets.

@p2yang
Created January 2, 2020 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p2yang/092d7a43fcfa046dc367935b55006912 to your computer and use it in GitHub Desktop.
Save p2yang/092d7a43fcfa046dc367935b55006912 to your computer and use it in GitHub Desktop.
debounce-throttle
export function debounce(fn, delay = 500) {
let timer = null
return function () {
clearTimeout(timer)
timeout = setTimeout(() => {
fn.apply(this, arguments)
}, delay)
}
}
export function throttle (fn, delay = 500) {
let lastTime = 0
return function () {
const now = Date.now()
if (now - lastTime > delay) {
fn.apply(this, arguments)
lastTime = now
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment