throttle
throttle节流函数, 可以限制函数的调用频率
🍱 参数
- fn- 函数, 可以是一个匿名函数, 也可以是一个已经存在的函数
- [delay]- 时间间隔, 单位是毫秒 (ms). 默认值是- 1000
🔥 返回值
throttle 返回一个函数, 这个函数可以被调用.
🚀 示例
import { throttle } from 'atools-js';
const fn = throttle(() => {
  console.log('throttle');
}, 1000); // 调用一次, 在 1000ms 后输出 throttle
💡 源码
source code
/atools/_basic/throttle.ts
export const throttle = (fn: Function, ms: number = 1000): Function => {
  let isRunning = false;
  return (...args: any[]) => {
    if (isRunning) return;
    isRunning = true;
    setTimeout(() => {
      fn(...args);
      isRunning = false;
    }, ms);
  };
};