Appearance
useThrottleFn
节流函数,固定时间间隔执行,适用于 resize、scroll 等高频事件。
函数签名
ts
function useThrottleFn<T extends FunctionArgs>(
fn: T,
ms?: MaybeRefOrGetter<number>,
trailing?: boolean,
leading?: boolean,
rejectOnCancel?: boolean,
): PromisifyFn<T>基础用法
参数
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| fn | 需要节流的函数 | T extends FunctionArgs | — |
| ms | 间隔时间(毫秒) | MaybeRefOrGetter<number> | 200 |
| trailing | 结束后是否再执行一次 | boolean | false |
| leading | 开始时是否立即执行 | boolean | true |
| rejectOnCancel | 取消时是否 reject Promise | boolean | false |
返回值
返回节流包装后的函数 PromisifyFn<T>,调用方式与原函数一致。
代码示例
ts
import { useThrottleFn } from 'ljf-hooks'
const throttledFn = useThrottleFn(() => {
// 节流逻辑
}, 1000)
window.addEventListener('resize', throttledFn)