WorkerGlobalScope: setTimeout() method
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
Note: This feature is only available inWeb Workers.
ThesetTimeout() method of theWorkerGlobalScope interface sets a timer which executes a function or specified piece of code once the timer expires.
In this article
Syntax
setTimeout(code)setTimeout(code, delay)setTimeout(functionRef)setTimeout(functionRef, delay)setTimeout(functionRef, delay, param1)setTimeout(functionRef, delay, param1, param2)setTimeout(functionRef, delay, param1, param2, /* …, */ paramN)Parameters
functionRefA
functionto be executed after the timer expires.codeAn alternative syntax that allows you to include a string instead of a function,which is compiled and executed when the timer expires. This syntax isnotrecommended for the same reasons that make using
eval()a security risk.delayOptionalThe time, in milliseconds that the timer should wait beforethe specified function or code is executed. If this parameter is omitted, a value of 0is used, meaning execute "immediately", or more accurately, the next event cycle.
Note that in either case, the actual delay may be longer than intended; seeReasons for delays longer than specified.
Also note that if the value isn't a number, implicittype coercion is silently done on the value to convert it to a number — which can lead to unexpected and surprising results; seeNon-number delay values are silently coerced into numbers for an example.
param1, …,paramNOptionalAdditional arguments which are passed through to the function specified by
functionRef.
Return value
ThesetTimeout() method returns a positive integer (typically within the range of 1 to 2,147,483,647) that uniquely identifies the timer created by the call. This identifier, often referred to as a "timeout ID", can be passed toclearTimeout() to cancel the timer.
Within the same global environment (e.g., a specific window or worker) the timeout ID is guaranteed not to be reused for any new timer as long as the original timer remains active. However, separate global environments maintain their own independent pools of timer IDs.
Description
SeeWindow.setTimeout() for detailed descriptions.
Examples
SeeWindow.setTimeout() for examples.
Specifications
| Specification |
|---|
| HTML> # dom-settimeout-dev> |