Skip to content

Function: throttle()

ts
function throttle<TArgs>(
   fn, 
   delay, 
   options?): (...args) => void;

Creates a throttled function that only invokes the original function at most once per every delay milliseconds. The throttled function has optional leading or trailing invocation. 创建一个节流函数,在每个 delay 毫秒内最多只调用一次原函数。 节流函数可以选择在前沿或后沿调用。

Type Parameters

Type Parameter
TArgs extends any[]

Parameters

ParameterTypeDescription
fn(...args) => voidThe original function to be throttled. / 要被节流的原函数
delaynumberThe number of milliseconds to throttle. / 节流的毫秒数
optionsThrottleOptionsOptional configuration for leading and/or trailing invocation. / 前沿和/或后沿调用的可选配置

Returns

Throttled function / 节流函数

(...args) => void

Example

ts
const results = []
const push = throttle((x) => results.push(x), 100, { leading: true })
push(1)
push(2)
push(3)
results // => [1] — only leading edge fires immediately

Since

1.2.0

Released under the MIT License.