Last active
March 2, 2021 19:14
-
-
Save Hwan-seok/ef5fdaa39eefffe2aa3167c4db0c30a4 to your computer and use it in GitHub Desktop.
debouncer
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
export const debouncer = ( | |
func: (...args: unknown[]) => unknown, | |
params: { [key: string]: unknown }, | |
debounceMilliseconds: number, | |
debounceId: string, | |
): unknown => { | |
const lastExecuteTimeFromString: string = window.localStorage.getItem(debounceId) | |
const lastExecuteTime = _.parseInt(lastExecuteTimeFromString) | |
const currentTime = new Date().getTime() | |
if (!lastExecuteTimeFromString || (lastExecuteTime && currentTime - lastExecuteTime > debounceMilliseconds)) { | |
window.localStorage.setItem(debounceId, currentTime.toString()) | |
return func(params) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment