Logos DX
    Preparing search index...

    Function throttle

    • Throttle a function, calling it at most once every delay milliseconds. The first call is executed immediately, subsequent calls within the delay window return the cached result from the last execution.

      Type Parameters

      Parameters

      • fn: T

        The function to throttle

      • opts: ThrottleOptions

        Throttle configuration options

      Returns ThrottledFunction<T>

      A throttled function with cancel capability

      When fn is not a function

      When delay is not a positive number

      When onThrottle is provided but not a function

      When throws is provided but not a boolean

      When throttling occurs and throws is true

      const throttledFn = throttle(fn, {
      delay: 1000,
      onThrottle: (args) => {
      console.log('throttled', args);
      },
      });

      throttledFn(); // calls fn immediately
      throttledFn(); // returns cached result
      await wait(500);
      throttledFn(); // returns cached result

      // Cancel clears the throttle state
      throttledFn.cancel();
      throttledFn(); // calls fn immediately (state reset)