Provides timing related higher-order functions.

This class should not be constructed as it only contains static methods.

Constructors

Methods

Constructors

Methods

  • Wraps a callback in a debounced timeout. Delay execution of the callback function until the function has not been called for the given delay in milliseconds.

    Type Parameters

    • Args

    Parameters

    • callback: ((...args: Args[]) => void)

      A function to execute once the debounced threshold has been passed.

        • (...args): void
        • Parameters

          Returns void

    • delay: number

      An amount of time in milliseconds to delay.

    Returns ((...args: Args[]) => void)

    A wrapped function that can be called to debounce execution.

      • (...args): void
      • Parameters

        Returns void

    /**
    * Debounce the update invocation by 500ms.
    */
    const updateDebounced = Timing.debounce(() => doc.update(), 500);

    // Use the function like:
    updateDebounced();
    /**
    * Debounce the update invocation by 500ms.
    *
    * @param {string} value - A value to update.
    */
    const updateDebounced = Timing.debounce((value) => doc.update(value), 500);

    // Use the function like:
    updateDebounced('new value');
  • Creates a double click event handler that distinguishes between single and double clicks. Calls the single callback on a single click and the double callback on a double click. The default double click delay to invoke the double callback is 400 milliseconds.

    Parameters

    • opts: {
          delay?: number;
          double?: ((event: Event) => void);
          single?: ((event: Event) => void);
      }

      Optional parameters.

      • Optionaldelay?: number

        Double click delay.

      • Optionaldouble?: ((event: Event) => void)

        Double click callback.

          • (event): void
          • Parameters

            Returns void

      • Optionalsingle?: ((event: Event) => void)

        Single click callback.

          • (event): void
          • Parameters

            Returns void

    Returns ((event: Event) => void)

    The gated double-click handler.

      • (event): void
      • Parameters

        Returns void

    // Given a button element.
    button.addEventListener('click', Timing.doubleClick({
    single: (event) => console.log('Single click: ', event),
    double: (event) => console.log('Double click: ', event)
    });