Documentation - v7.17.0
    Preparing search index...

    Function throttle

    • Wraps a function so that calls are grouped into throttle windows of wait milliseconds. Mirrors lodash's throttle API.

      A window starts on the first call made outside of a window. fn is invoked at most once at the start of the window (leading) and at most once at its end with the latest arguments (trailing). Unlike lodash, the trailing invocation does not start a new window: a call made right after it starts a new window and may invoke fn again immediately.

      Type Parameters

      • T extends (...args: any[]) => void

      Parameters

      • fn: T

        The function to throttle.

      • wait: number

        The duration of a throttle window, in milliseconds.

      • Optionaloptions: { leading?: boolean; trailing?: boolean }

        Throttle behavior options.

        • Optionalleading?: boolean

          Invokes fn immediately on the first call of a throttle window. Defaults to true.

        • Optionaltrailing?: boolean

          Invokes fn once more at the end of the window if calls happened during it. Defaults to true.

      Returns { cancel: () => void; throttled: (...parameters: Parameters<T>) => void }

      An object with throttled, the throttled function to call instead of fn, and cancel, which cancels any pending trailing invocation and resets the throttle window.