Created
June 18, 2020 11:22
-
-
Save bluwy/a1e3515a784849a225b568411910bf05 to your computer and use it in GitHub Desktop.
Simple debounce and throttle with proper context binding
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(fn, wait) { | |
let t | |
return function () { | |
clearTimeout(t) | |
t = setTimeout(() => fn.apply(this, arguments), wait) | |
} | |
} |
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(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