Skip to content

Home > robinson > throttle

throttle() function

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

节流函数,限制函数调用的频率。

Signature:

typescript
declare function throttle(fn: Function, immediate: number, leading?: boolean, trailing?: boolean): Function;
declare function throttle(fn: Function, immediate: number, leading?: boolean, trailing?: boolean): Function;

Parameters

ParameterTypeDescription
fnFunction“fn”参数是您想要限制的函数。
immediatenumber“immediate”参数是函数调用之间的最小时间间隔(以毫秒为单位)。
leadingboolean(Optional) “leading”参数决定该函数是在调用时立即执行还是等待指定的“immediate”时间过去后才第一次执行该函数。
trailingboolean(Optional) “trailing”参数决定是否应在上次调用后立即调用节流函数,即使指定的时间间隔(“immediate”)尚未过去。

Returns:

Function

{Function} - 返回一个节流函数。

Example

JavaScript
const log = () => console.log('Logged');
const throttledLog = throttle(log, 1000, true, false);
throttledLog(); // 立即执行
throttledLog(); // 1秒内再次调用不会执行
throttledLog.cancel(); // 取消执行
const log = () => console.log('Logged');
const throttledLog = throttle(log, 1000, true, false);
throttledLog(); // 立即执行
throttledLog(); // 1秒内再次调用不会执行
throttledLog.cancel(); // 取消执行

Released under the MIT License.