Skip to content

Instantly share code, notes, and snippets.

@slugbyte
Last active November 14, 2017 23:31
Show Gist options
  • Save slugbyte/3a5619108725a917d439ca73f9d86354 to your computer and use it in GitHub Desktop.
Save slugbyte/3a5619108725a917d439ca73f9d86354 to your computer and use it in GitHub Desktop.
// great for perioticly limiting events
throttle = (fn, ms) => {
let ready = true;
return (...args) => {
if(ready){
ready = false
setTimeout(() => ready = true, ms)
return fn(...args)
}
}
}
// great for delaying until the last event
debounce = (fn, ms) => {
let timeout;
return (...args) => {
if(timeout) clearTimeout(timeout)
timeout = setTimeout(() => {
timeout = null
fn(...args)
}, ms)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment