Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. WorkerGlobalScope
  4. setInterval()

WorkerGlobalScope: setInterval() 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 available inWeb Workers.

Warning:When thecode parameter is used, this method dynamically executes its value as JavaScript.APIs like this are known asinjection sinks, and are potentially a vector forcross-site-scripting (XSS) attacks.

You can mitigate this risk by always assigningTrustedScript objects instead of strings andenforcing trusted types.SeeSecurity considerations inWindow.setInterval() for more information.

ThesetInterval() method of theWorkerGlobalScope 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 usingclearInterval().SeeWindow.setInterval() for more information.

Note that if you wish to have your function calledonce after the specified delay, usesetTimeout().

Syntax

js
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

Afunction to be executed everydelay milliseconds.The first execution happens afterdelay milliseconds.

code

ATrustedScript or a string of arbitrary code that is compiled and executed everydelay milliseconds.This can be used instead of passing a function, but isstrongly discouraged for the same reasons that make usingeval() a security risk.

delayOptional

The delay time between executions of the specified function or code, in milliseconds.Defaults to 0 if not specified.SeeDelay restrictions inWindow.setInterval for details on the permitted range ofdelay values.

arg1, …,argNOptional

Additional arguments which are passed through to the function specified byfunc 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 toclearInterval() to stop the repeated execution of the specified function.

Exceptions

SyntaxError

Thecode can't be parsed as a script.

TypeError

Thrown if thecode parameter is set to a string whenTrusted Types areenforced 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.

Examples

SeesetInterval() for examples.

Specifications

Specification
HTML
# dom-setinterval-dev

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp