setTimeout() global function

The setTimeout method sets a timer that executes the specified function when it expires.

Syntax

setTimeout(function: () => void, delay?: number): number;

Parameters

function

The function to be executed after the expiration time.

delay (Optional)

The number of milliseconds of delay after which the function call will occur. If this parameter is omitted, delay takes the default value of 0, specifying that the function will be executed after the execution of existing tasks queued in the js thread task queue.

The js thread may be frozen while the app is in the background. As a result, the actual delay of setTimeout may be longer than the specified value. Also, the duration of the frozen js thread will still be counted in the delay of setTimeout.

Return Value

Returns a positive integer representing the ID value of the timer. This ID value can be used to cancel the timer.

The front-end code should not make any assumptions about the actual value of the returned ID.

Example

Compatibility

LCD tables only load in the browser

Except as otherwise noted, this work is licensed under a Creative Commons Attribution 4.0 International License, and code samples are licensed under the Apache License 2.0.