WorkerGlobalScope: setInterval() method
{{APIRef("HTML DOM")}} {{AvailableInWorkers("window_and_worker")}}
[!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
{{domxref("TrustedScript")}}objects instead of strings and enforcing trusted types. See Security considerations inWindow.setInterval()for more information.
The setInterval() method of the {{domxref("WorkerGlobalScope")}} interface repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.
It is commonly used to set a delay for functions that are executed again and again, such as animations.
You can cancel the interval using {{domxref("WorkerGlobalScope.clearInterval", "clearInterval()")}} .
See {{domxref("Window.setInterval()")}} for more information.
Note that if you wish to have your function called once after the specified delay, use {{domxref("WorkerGlobalScope.setTimeout", "setTimeout()")}} .
Syntax
setInterval(code)
setInterval(code, delay)
setInterval(func)
setInterval(func, delay)
setInterval(func, delay, arg1)
setInterval(func, delay, arg1, arg2)
setInterval(func, delay, arg1, arg2, /* …, */ argN)
Parameters
func- : A
{{jsxref("function")}}to be executed everydelaymilliseconds. The first execution happens afterdelaymilliseconds.
- : A
code- : A
{{domxref("TrustedScript")}}or 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 using{{jsxref("Global_Objects/eval", "eval()")}}a security risk.
- : A
delay{{optional_inline}}- : The delay time between executions of the specified function or code, in milliseconds.
Defaults to 0 if not specified.
See Delay restrictions in
Window.setIntervalfor details on the permitted range ofdelayvalues.
- : The delay time between executions of the specified function or code, in milliseconds.
Defaults to 0 if not specified.
See Delay restrictions in
arg1, …,argN{{optional_inline}}- : Additional arguments which are passed through to the function specified by func once the timer expires.
Return value
A positive integer (typically within the range of 1 to 2,147,483,647) that uniquely identifies the interval timer created by the call.
This identifier, often referred to as an “interval ID”, can be passed to {{domxref("WorkerGlobalScope.clearInterval", "clearInterval()")}} to stop the repeated execution of the specified function.
Exceptions
{{jsxref("SyntaxError")}}- : The
codecan’t be parsed as a script.
- : The
{{jsxref("TypeError")}}- : Thrown if the
codeparameter is set to a string when Trusted Types are enforced by 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
Examples
See {{domxref("Window.setInterval", "setInterval()")}} for examples.
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- Polyfill of
setIntervalwhich allows passing arguments to the callback incore-js {{domxref("Window.setInterval()")}}{{domxref("WorkerGlobalScope.clearInterval()")}}{{domxref("WorkerGlobalScope.setTimeout()")}}{{domxref("DedicatedWorkerGlobalScope.requestAnimationFrame()")}}