useThrottleFn
节流函数的执行。 特别适用于限制调整大小和滚动等事件处理程序的执行速率。
节流就像一个弹簧,可以投掷球:当一个球飞出后,它需要一些时间收缩回来,所以除非准备就绪,否则它无法投掷更多的球。
演示
此演示的延迟设置为 1000 毫秒。
按钮点击次数:0
事件处理程序调用次数:0
用法
js
import { useThrottleFn } from '@vueuse/core'
const throttledFn = useThrottleFn(() => {
// do something, it will be called at most 1 time per second
}, 1000)
useEventListener(window, 'resize', throttledFn)
推荐阅读
类型声明
显示类型声明
typescript
/**
* Throttle execution of a function. Especially useful for rate limiting
* execution of handlers on events like resize and scroll.
*
* @param fn A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
* to `callback` when the throttled-function is executed.
* @param ms A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
* (default value: 200)
*
* @param [trailing] if true, call fn again after the time is up (default value: false)
*
* @param [leading] if true, call fn on the leading edge of the ms timeout (default value: true)
*
* @param [rejectOnCancel] if true, reject the last call if it's been cancel (default value: false)
*
* @return A new, throttled, function.
*/
export declare function useThrottleFn<T extends FunctionArgs>(
fn: T,
ms?: MaybeRefOrGetter<number>,
trailing?: boolean,
leading?: boolean,
rejectOnCancel?: boolean,
): PromisifyFn<T>
源代码
贡献者
更新日志
v12.8.0
于 2025/3/5v10.0.0-beta.4
于 2023/4/134d757
- feat(types)!: 将 MaybeComputedRef
重命名为 MaybeRefOrGetter