Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Window setInterval() Method

Window setInterval() Method

Window Object Reference Window Object

Example

Pop up "Hello" every three seconds (3000 milliseconds):

setInterval

(

function

(

)

{

alert

(

"

Hello

"

)

;

}

,

3000

)

;

Try it »

Using a code string:

setInterval

(

'

alert("Hello");

'

,

3000

)

;

Try it »


Definition and Usage

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).

The setInterval() method keeps calling a function until clearInterval() it is called or the window is closed. The ID value returned by setInterval() can be passed as a parameter to the clearInterval() method.

Tip: 1000 milliseconds = 1 second.

Tip: If you only want to execute it once, you can use setTimeout() method.


Browser support

The numbers in the table indicate the first browser version that supports this property.

Method
setInterval() 1.0 4.0 1.0 1.0 4.0

Syntax



setInterval(code, milliseconds);

setInterval(function, milliseconds, param1, param2, ...)

Parameters Description
code/function Required. The code string to be called, or a function.
milliseconds Required. The time interval, in milliseconds, between each execution or call of the code/function.
param1, param2, ... Optional. Additional arguments passed to the executed function (not supported by IE9 and earlier versions).

Technical Details

Return value: Returns an ID (a number) that can be passed to clearInterval() or clearTimeout() to cancel the execution.

More examples

Example

You can call a named function to pop up "Hello" every three seconds (3000 milliseconds):

var

myVar

;

function

myFunction

(

)

{

myVar

=

setInterval

(

alertFunc

,

3000

)

;

}

function

alertFunc

(

)

{

alert

(

"

Hello!

"

)

;

}

Try it »

Example

Display the current time (the setInterval() method executes a function every second, like a watch):

var

myVar

=

setInterval

(

function

(

)

{

myTimer

(

)

}

,

1000

)

;

function

myTimer

(

)

{

var

d

=

new

Date

(

)

;

var

t

=

d

.

toLocaleTimeString

(

)

;

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

t

;

}

Try it »

Example

Use clearInterval() to stop the execution of setInterval:

var

myVar

=

setInterval

(

function

(

)

{

myTimer

(

)

}

,

1000

)

;

function

myTimer

(

)

{

var

d

=

new

Date

(

)

;

var

t

=

d

.

toLocaleTimeString

(

)

;

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

t

;

}

function

myStopFunction

(

)

{

clearInterval

(

myVar

)

;

}

Try it »

Example

Use setInterval() and clearInterval() to create a dynamic progress bar:

function

move

(

)

{

var

elem

=

document

.

getElementById

(

"

myBar

"

)

;

var

width

=

0

;

var

id

=

setInterval

(

frame

,

10

)

;

function

frame

(

)

{

if

(

width

==

100

)

{

clearInterval

(

id

)

;

}

else

{

width

++;

elem

.

style

.

width

=

width

'

%

'

;

}

}

}

Try it »

Example

Switch the background color every 300 milliseconds:

var

myVar

=

setInterval

(

function

(

)

{

setColor

(

)

}

,

300

)

;

function

setColor

(

)

{

var

x

=

document

.

body

;

x

.

style

.

backgroundColor

=

x

.

style

.

backgroundColor

==

"

yellow

"

?

"

pink

"

:

"

yellow

"

;

}

function

stopColor

(

)

{

clearInterval

(

myVar

)

;

}

Try it »

Example

Pass arguments to the alertFunc function (not supported by IE9 and earlier versions):

var

myVar

;

function

myStartFunction

(

)

{

myVar

=

setInterval

(

alertFunc

,

2000

,

"

Runoob

"

,

"

Google

"

)

;

}

Try it »

However, if an anonymous function is used, all browsers support it:

var

myVar

;

function

myStartFunction

(

)

{

myVar

=

setInterval

(

function

(

)

{

alertFunc

(

"

Runoob

"

,

"

Google

"

)

;

}

,

2000

)

;

}

Try it »


Related pages

Window object: clearInterval() method

Window object: setTimeout() method

Window object: clearTimeout() method

Window Object Reference Window Object