apphouse
Version:
Component library for React that uses observable state management and theme-able components.
26 lines (23 loc) • 844 B
text/typescript
/**
* Creates a debounced function that delays the execution of the provided
* function by a specified wait time.
*
* @template T - The tuple of arguments for the debounced function.
* @param {(...args: T) => void} func - The function to be debounced.
* @param {number} wait - The number of milliseconds to delay the execution of the function after the last invocation.
* @returns {(...args: T) => void} - Returns the debounced function.
*/
export function debounce(
func: { (): void; apply?: any },
wait: number | undefined
) {
let timeoutId: string | number | NodeJS.Timeout | undefined;
return function (...args: any) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
func.apply(this, args);
}, wait);
};
}