Skip to content

useThrottleFnWithLock

useThrottleFnWithLock(fn, options?): Object

使用节流函数和锁定函数实现带有锁定功能的函数 该函数封装了 ahooks 的 useThrottleFn 和 useLockFn,确保函数在特定时间内只执行一次。此外,通过引入锁机制,可以确保函数在上一次执行未完成之前不会被再次触发

参数

NameTypeDefault valueDescription
fnFunctionundefined被节流的函数。
options?Object{}节流函数的配置选项。
options.leadingundefined | booleantrue是否在节流开始时立即执行,默认为true。
options.trailingundefined | booleanfalse是否在节流结束时执行,默认为false。
options.waitundefined | number3000节流的时间间隔,默认为3000毫秒。

返回值

Object

返回一个节流处理后的函数

示例

ts
function MyComponent() {
 const {
   run: handleAction,
   cancel,
   flush,
 } = useThrottleFnWithLock(
   event => {
     // 在这里执行你的处理逻辑
     console.log('处理事件', event)
   },
   {wait: 1000, leading: true, trailing: false}
 )

 return <Button onClick={handleAction}>点击我</Button>
}

源码

use-throttle-fn-with-lock.js