debounce.ts 356 B

123456789101112
  1. export default function debounce<T extends (...args: any[]) => void>(
  2. func: T,
  3. wait: number
  4. ): T {
  5. let timeout: ReturnType<typeof setTimeout> | null = null;
  6. return function (this: any, ...args: Parameters<T>) {
  7. if (timeout !== null) {
  8. clearTimeout(timeout);
  9. }
  10. timeout = setTimeout(() => func.apply(this, args), wait);
  11. } as T;
  12. }