Last active
January 2, 2018 18:37
-
-
Save joakin/0890e13047d63361032fc6c27839f352 to your computer and use it in GitHub Desktop.
Small debounce and throttle fns.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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