m3-academy-template-vtexio/custom/react/utils/debounce.ts
Caroline Moran f2a03c53ae first commit
2022-08-11 09:06:07 -04:00

15 lines
379 B
TypeScript

export const debounce = <T extends (...args: any[]) => any>(
callback: T,
waitFor: number
): any => {
let timeout: any = 0;
return (...args: Parameters<T>): ReturnType<T> => {
let result: any;
clearTimeout(timeout);
timeout = setTimeout(() => {
result = callback(...args);
}, waitFor);
return result;
};
};