common-stuff
    Preparing search index...

    Function throttle

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

      The returned function exposes .cancel() to discard the pending trailing call.

      Type Parameters

      • A extends unknown[]

      Parameters

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

        The function to be throttled.

      • timeInMs: number

        The number of milliseconds to throttle invocations to.

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

        Options object.

        • Optionalleading?: boolean

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

          true

        • Optionaltrailing?: boolean

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

          true

      Returns Cancellable<(this: unknown, ...args: A) => void>

      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