Wagtail client-side components
    Preparing search index...

    Function debounce

    • Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked. The debounced function comes with a cancel method to cancel delayed func invocations.

      If wait is provided as a non-number value the function will be invoked immediately.

      When the debounced function is called it returns a promise that resolves with the result of the invoked func or a rejected promise if calling the function throws an error.

      Type Parameters

      • A extends any[]
      • R = any

      Parameters

      • func: (...args: A) => R | Promise<R>
      • wait: null | number = 0

      Returns DebouncedFunction<A, R>

      const debounced = debounce(() => console.log('Hello World!'), 1000);
      debounced(); // logs 'Hello World!' after 1 second
      const debounced = debounce(() => console.log('Hello World!'), null);
      debounced(); // logs 'Hello World!' immediately
      const debounced = debounce(() => window.screen, 500);
      debounced().then((screen) => console.log(screen)); // returns current screen value after 500ms
      const debounced = debounce(() => window.nothingHere.alsoNothing, 500);
      debounced().catch((error) => console.log(error)); // logs error (TypeError: window.nothingHere is undefined) after 500ms