index.ts 407 B

123456789101112131415161718
  1. // @ts-nocheck
  2. type Timeout = ReturnType<typeof setTimeout> | null;
  3. /**
  4. * 防抖
  5. * @param fn 回调函数
  6. * @param wait 延迟时间
  7. * @returns
  8. */
  9. export function debounce(fn : (...args : any[]) => void, wait = 300) {
  10. let timer : Timeout = null;
  11. return function (this : any, ...args : any[]) {
  12. if (timer) clearTimeout(timer);
  13. timer = setTimeout(() => {
  14. fn.apply(this, args);
  15. }, wait);
  16. };
  17. }