Function throttle

Limits the rate at which a function can be called.

Throttling ensures a function is called at most once per duration period. During the throttle period, subsequent calls return the last computed result.

const a = throttle(1000, (x: number) => x * 2);
a(1); // 2 (executes immediately)
a(2); // 2 (returns previous result, function not called)
// After 1000ms
a(3); // 6 (executes again)
  • Type Parameters

    • Fn extends AnyFn

      The type of the function to be throttled.

    Parameters

    • duration: number

      The time in milliseconds to wait before allowing the next execution.

    • fn: Fn

      The function to be throttled.

    Returns (...args: Parameters<Fn>) => ReturnType<Fn>

    A throttled function that returns the result of the throttled function.