Skip to content

Instantly share code, notes, and snippets.

@bluwy
Created June 18, 2020 11:22
Show Gist options
  • Save bluwy/a1e3515a784849a225b568411910bf05 to your computer and use it in GitHub Desktop.
Save bluwy/a1e3515a784849a225b568411910bf05 to your computer and use it in GitHub Desktop.
Simple debounce and throttle with proper context binding
function debounce(fn, wait) {
let t
return function () {
clearTimeout(t)
t = setTimeout(() => fn.apply(this, arguments), wait)
}
}
function throttle(fn, wait) {
let t
return function () {
if (t == null) {
fn.apply(this, arguments)
t = setTimeout(() => (t = undefined), wait)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment