Last active
July 8, 2023 18:58
-
-
Save SheryConcepts/c5175638d2f2fb8ba3b1524bb8e5b81c to your computer and use it in GitHub Desktop.
Typescript Debouncing Utility Function.
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
type FunctionType = (...args: any[]) => any; | |
function debounced<T extends FunctionType>( | |
func: T, | |
duration: number | |
): (...args: Parameters<T>) => ReturnType<T> { | |
let timeoutId: NodeJS.Timeout; | |
let result: ReturnType<T>; | |
return (...args: Parameters<T>) => { | |
timeoutId && clearTimeout(timeoutId); | |
timeoutId = setTimeout(() => { | |
result = func(...args); | |
}, duration); | |
return result; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment