Function throttle

  • Creates a throttled function that will execute func at most once per timeInMs interval.

    Type Parameters

    • A extends unknown[]

    Parameters

    • func: ((...args) => void)

      The function to be throttled.

        • (...args): void
        • Parameters

          • Rest ...args: A

          Returns void

    • timeInMs: number

      The number of milliseconds to throttle invocations to.

    • Optional options: {
          leading?: boolean;
          trailing?: boolean;
      }

      Options object.

      • Optional leading?: boolean

        If true the func will be executed on the leading edge of the timeout.

        Default Value

        true

      • Optional trailing?: boolean

        If true the func will be executed on the trailing edge of the timeout.

        Default Value

        true

    Returns ((...args) => void)

      • (...args): void
      • Parameters

        • Rest ...args: A

        Returns void

    Example

    const throttled = throttle(() => console.log('throttled'), 100)
    throttled() // will be called immediately
    throttled() // will be ignored
    await delay(100)
    throttled() // will be called after 100ms