Created
January 7, 2020 04:57
-
-
Save treyhuffine/e7f24828455bbb9d7e232c7d2df9e1d9 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
const throttle = (callback, delay) => { | |
let throttleTimeout = null; | |
let storedEvent = null; | |
const throttledEventHandler = event => { | |
storedEvent = event; | |
const shouldHandleEvent = !throttleTimeout; | |
if (shouldHandleEvent) { | |
callback(storedEvent); | |
storedEvent = null; | |
throttleTimeout = setTimeout(() => { | |
throttleTimeout = null; | |
if (storedEvent) { | |
throttledEventHandler(storedEvent); | |
} | |
}, delay); | |
} | |
}; | |
return throttledEventHandler; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment