Created
January 2, 2020 14:04
-
-
Save p2yang/092d7a43fcfa046dc367935b55006912 to your computer and use it in GitHub Desktop.
debounce-throttle
This file contains 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
export function debounce(fn, delay = 500) { | |
let timer = null | |
return function () { | |
clearTimeout(timer) | |
timeout = setTimeout(() => { | |
fn.apply(this, arguments) | |
}, delay) | |
} | |
} | |
export function throttle (fn, delay = 500) { | |
let lastTime = 0 | |
return function () { | |
const now = Date.now() | |
if (now - lastTime > delay) { | |
fn.apply(this, arguments) | |
lastTime = now | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment