R
fn

throttle

v0.0.10tested

Invokes a function at most once per `wait` ms

Example

ts
const onScroll = throttle(() => update(), 100);
window.addEventListener('scroll', onScroll);

Signature

ts
export function throttle<T extends AnyFunction>(fn: T, wait: number | (() => number), options: ThrottleOptions ={ ... }

Type Parameters

Textends AnyFunction

Parameters

ParameterTypeDescription
fnTThe function to throttle
waitnumber | (() => number)Milliseconds to throttle to, or a getter resolved lazily on each call (useful for reactive windows)
options?ThrottleOptionsLeading/trailing edge behavior

Returns

ThrottledFunction<T>The throttled function with `cancel()`, `flush()` and `pending()`