Window setInterval() Method
Window Object Reference Manual
Example
Pop up "Hello" every three seconds (3000 milliseconds):
setInterval(function(){alert("Hello"); }, 3000);
Try it yourself »
Using a code string:
setInterval('alert("Hello");', 3000);
Try it yourself »
Definition and Usage
The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).
The setInterval() method continues calling the function until clearInterval() is called or the window is closed. The ID returned from setInterval() can be used as a parameter for the clearInterval() method.
Note: 1000 milliseconds = 1 second.
Note: If you want to execute something only once, use the setTimeout() method.
Browser Support
| Method | Internet Explorer | Mozilla Firefox | Chrome | Safari | Opera |
|---|---|---|---|---|---|
| setInterval() | 1.0 | 4.0 | 1.0 | 1.0 | 4.0 |
Syntax
setInterval(code, milliseconds); setInterval(function, milliseconds, param1, param2, ...)
| Parameter | Description |
|---|---|
| code/function | Required. The code to run, or a function to execute. |
| milliseconds | Required. The interval between each execution of the code or function, in milliseconds. |
| param1, param2, ... | Optional. Additional parameters passed to the executing function (not supported in Internet Explorer 9 and earlier versions). |
Technical Details
Returns:
- Returns an ID (number), which can be passed to clearInterval(), clearTimeout() to cancel execution.
More Examples
Example
You can pop up "Hello" every three seconds (3000 milliseconds) by calling a named function:
var myVar;
function myFunction(){
myVar = setInterval(alertFunc, 3000);
}
function alertFunc(){
alert("Hello!");
}
Try it yourself »
Example
Display current time (the setInterval() method will call the function once per second, similar to a watch feature):
var myVar = setInterval(function(){myTimer()}, 1000);
function myTimer(){
var d = new Date();
var t = d.toLocaleTimeString();
document.getElementById("demo").innerHTML = t;
}
Try it yourself »
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 yourself »
Example
Create a dynamic progress bar using setInterval() and clearInterval():
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 yourself »
Example
Change 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 yourself »
Example
Pass parameters to the alertFunc function (not supported in Internet Explorer 9 and earlier versions):
var myVar;
function myStartFunction(){
myVar = setInterval(alertFunc, 2000, "", "Google");
}
Try it yourself »
However, if you use an anonymous function, all browsers support it:
var myVar;
function myStartFunction(){
myVar = setInterval(function(){alertFunc("", "Google"); }, 2000);
}
Try it yourself »
YouTip