web api instance method
WorkerGlobalScope: setTimeout() method
Available in workers
[!WARNING] When the
codeparameter is used, this method dynamically executes its value as JavaScript. APIs like this are known as injection sinks, and are potentially a vector for cross-site-scripting (XSS) attacks.You can mitigate this risk by always assigning
TrustedScriptobjects instead of strings and enforcing trusted types. See Security considerations inWindow.setTimeout()for more information.
The setTimeout() method of the WorkerGlobalScope interface sets a timer which executes a function or specified piece of code once the timer expires.
Syntax
setTimeout(code)
setTimeout(code, delay)
setTimeout(func)
setTimeout(func, delay)
setTimeout(func, delay, param1)
setTimeout(func, delay, param1, param2)
setTimeout(func, delay, param1, param2, /* …, */ paramN)
Parameters
-
func- : A
functionto be executed after the timer expires.
- : A
-
code- : A
TrustedScriptor a string of arbitrary code that is compiled and executed everydelaymilliseconds. This can be used instead of passing a function, but is strongly discouraged for the same reasons that make usingeval()a security risk.
- : A
-
delayOptional-
: The time that the timer should wait before the specified function or code is executed, in milliseconds. Defaults to 0 if not specified.
Note:
- The delay has a maximum value of 2147483647 ms — specifying larger values may result in overflow or a 0 value being used. See maximum delay value for more information.
- The actual delay may be longer than set.
For example, setting
delayto 0 will execute in the next event cycle rather than “immediately”. See Reasons for longer delays than specified for more information. - If the value isn’t a number, implicit type coercion is silently done on the value to convert it to a number. This can lead to unexpected and surprising results — see Non-number delay values are silently coerced into numbers for an example.
-
-
param1, …,paramNOptional- : Additional arguments which are passed through to the function specified by
func.
- : Additional arguments which are passed through to the function specified by
Return value
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 to clearTimeout() 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.
Exceptions
SyntaxError- : The
codecan’t be parsed as a script.
- : The
TypeError- : Thrown if the
codeparameter is set to a string when Trusted Types are enforced by a CSP and no default policy is defined. It is also thrown if the first parameter is not one of the supported types: a function, string orTrustedScript.
- : Thrown if the
Description
See setTimeout() for detailed descriptions.
Examples
See setTimeout() for examples.