Last active
March 30, 2019 06:21
-
-
Save zerosrat/9c0549cd34d4a44c5d2e3e769efc09e7 to your computer and use it in GitHub Desktop.
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, ms, immediate) { | |
let id = null | |
return function(...args) { | |
id && clearTimeout(id) | |
if (immediate) { | |
const callNow = id === null | |
id = setTimeout(() => id = null, ms) | |
callNow && fn.apply(this, args) | |
} else { | |
id = setTimeout(() => { | |
fn.apply(this, args) | |
}, ms) | |
} | |
} | |
} |
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, ms) { | |
let id = null | |
return function(...args) { | |
if (!id) { | |
id = setTimeout(() => { | |
id = null | |
}, ms) | |
fn.apply(this, args) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment