Created
February 1, 2019 11:32
-
-
Save electerious/117529b582d5957411fe0b6c2c6faa8b to your computer and use it in GitHub Desktop.
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
const throttle = (fn, threshold) => { | |
let last | |
let timeout | |
return (...args) => { | |
const now = Date.now() | |
const shouldDelay = last && now < last + threshold | |
const delay = shouldDelay === true ? threshold - (now - last) : 0 | |
const execute = () => { | |
last = now | |
fn(...args) | |
} | |
clearTimeout(timeout) | |
timeout = setTimeout(execute, delay) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: