Skip to content

Instantly share code, notes, and snippets.

@joakin
Last active January 2, 2018 18:37
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 joakin/0890e13047d63361032fc6c27839f352 to your computer and use it in GitHub Desktop.
Save joakin/0890e13047d63361032fc6c27839f352 to your computer and use it in GitHub Desktop.
Small debounce and throttle fns.
function debounce (f, t) {
let tid = null
let r = _ => { tid = null; f() }
return _ => {
if (tid) clearTimeout(tid)
tid = setTimeout(r, t)
return () => { clearTimeout(tid); tid = null }
}
}
function throttle (f, t) {
let tid
let r = _ => { tid = null; f() }
return _ => {
if (!tid) tid = setTimeout(r, t)
return _ => { clearTimeout(tid); tid = null }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment