Skip to content

debounce(fn, wait, options)

防抖函数

入参

ts
interface IDebounceOpts {
  // default: false 开始时是否调用函数
  leading?: boolean;
  // default: true 结束时是否调用函数
  trailing?: boolean;
}
typescript
type debounce<T extends Function> = (
  // 需要防抖的函数
  fn: T,

  // 防抖时间
  wait: number,

  // 额外配置
  options?: IThrottleOpts
) => T;

示例

ts
const logNow = () => {
  console.log(Date.now());
};

const debounceFn = debounce(logNow, 1000);
while (true) {
  // 每隔一秒调用 logNow
  debounceFn();
}