Skip to content

Instantly share code, notes, and snippets.

@SheryConcepts
Last active July 8, 2023 18:58
Show Gist options
  • Save SheryConcepts/c5175638d2f2fb8ba3b1524bb8e5b81c to your computer and use it in GitHub Desktop.
Save SheryConcepts/c5175638d2f2fb8ba3b1524bb8e5b81c to your computer and use it in GitHub Desktop.
Typescript Debouncing Utility Function.
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