ts 版节流函数

// 节流函数
/* eslint-disable prefer-rest-params */
export function throttle(func, wait) {
  let timeout: NodeJS.Timeout | null = null;
  // eslint-disable-next-line func-names
  return function (this: unknown, ...args) {
     // eslint-disable-next-line @typescript-eslint/no-this-alias
       const context = this;
         // const args = arguments;
         if (!timeout) {
             timeout = setTimeout(() => {
                 timeout = null;
                 func.apply(context, args);
             }, wait);
         }
      };
}        

 

posted @ 2023-05-05 11:23  anin  阅读(211)  评论(0编辑  收藏  举报