ts 版节流函数

发布时间 2023-05-05 11:27:03作者: anin
// 节流函数
/* 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);
         }
      };
}