Function normalizedTimeoutCallback

  • Launches a function after a specified delay. If the delay is zero, the callback is executed immediately (synchronously).

    Parameters

    • callback: Function

      The function to be executed after the delay.

    • delay: number

      The amount of time (in milliseconds) to wait before executing the callback. If the delay is 0, the callback will be executed synchronously.

    Returns {
        clear: (() => void);
    }

    An object with a clear method that can be used to cancel the timeout.

    • clear: (() => void)
        • (): void
        • Clears the timeout if it was set.

          Returns void

    Example

    // Executes the callback after 1 second (1000 milliseconds).
    normalizedTimeoutCallback(() => {
    console.log('Executed after 1 second');
    }, 1000);

    // Executes the callback immediately.
    normalizedTimeoutCallback(() => {
    console.log('Executed synchronously');
    }, 0);