跳到主要内容

useThrottleFn

分类
导出大小
471 B
上次更改
3 个月前
相关

限制函数执行的频率。这对于限制诸如 resize 和 scroll 等事件的处理函数执行频率特别有用。

节流(Throttle)就像一个弹簧,它会抛出球:一个球飞出后,需要一些时间才能收缩回来,所以除非它准备好了,否则不能再抛出任何球。

演示

此演示的延迟设置为 1000 毫秒。

按钮点击次数:0

事件处理程序调用次数:0

用法

ts
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
)

类型声明

显示类型声明
ts
/**
 * 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.
 *
 * @__NO_SIDE_EFFECTS__
 */
export declare function 
useThrottleFn
<
T
extends
FunctionArgs
>(
fn
:
T
,
ms
?:
MaybeRefOrGetter
<number>,
trailing
?: boolean,
leading
?: boolean,
rejectOnCancel
?: boolean,
):
PromisifyFn
<
T
>

来源

源代码演示文档

贡献者

Anthony Fu
SerKo
Anthony Fu
IceMooncake
Thimo Sietsma
IlyaL
Fernando Fernández
OrbisK
Ctibor Laky
丶远方
vaakian X
azaleta
webfansplz
Jakub Freisler
Roman Harmyder
wheat

更新日志

v13.6.0
d32f8 - refactor: 为所有纯函数添加 @__NO_SIDE_EFFECTS__ 注释 (#4907)
v12.8.0
7432f - feat(types): 废弃 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)

根据 MIT 许可证发布。