Display "Hello" every second (1000 milliseconds):
setInterval(function () {element.innerHTML += "Hello"}, 1000);
Try it Yourself »
Call displayHello every second:
setInterval(displayHello, 1000);
Try it Yourself »
More examples below.
Description
The
setInterval()
method calls a function at specified intervals (in milliseconds).
The
setInterval()
method continues calling the function until
clearInterval()
is called, or the window is closed.
1 second = 1000 milliseconds.
To execute the function only once, use the
setTimeout()
method instead.
To clear an interval, use the
id
returned from setInterval():
myInterval = setInterval(
function
,
milliseconds
);
Then you can to stop the execution by calling clearInterval():
clearInterval(myInterval);
See Also:
The clearInterval() Method
The setTimeout() Method
The clearTimeout() Method
Syntax
setInterval(
function, milliseconds, param1, param2, ...
)
Parameters
Parameter
Description
function
Required.
The function to execute
milliseconds
Required.
The execution interval.
If the value is less than 10, 10 is used
param1, param2, ...
Optional.
Additional parameters to pass to the
function
Not supported in IE9 and earlier.
Return Value
Description
A numberThe id of the timer.
Use this id with clearInterval() to cancel the timer.
{
const date = new Date();
document.getElementById("demo").innerHTML = date.toLocaleTimeString();
}
Try it Yourself »
Example
Using clearInterval() to stop the digital watch:
const myInterval = setInterval(myTimer, 1000);
function myTimer() {
const date = new Date();
document.getElementById("demo").innerHTML = date.toLocaleTimeString();
}
function myStopFunction()
{
clearInterval(myInterval);
}
Try it Yourself »
Example
Using setInterval() and clearInterval() to create a dynamic progress bar:
function move() {
const element = document.getElementById("myBar");
let width = 0;
let id = setInterval(frame, 10);
function frame() {
if (width == 100) {
clearInterval(id);
} else {
width++;
element.style.width = width + '%';
}
}
}
Try it Yourself »
Example
Toggle between two background colors once every 500 milliseconds:
const myInterval = setInterval(setColor, 500);
function setColor() {
let x = document.body;
x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow";
}
function stopColor() {
clearInterval(myInterval);
}
Try it Yourself »
Example
Pass parameters to the function (does not work in IE9 and earlier):
setInterval(myFunc, 2000, "param1", "param2");
Try it Yourself »
However, if you use an anonymous function it works in all browsers:
setInterval(function() {myFunc("param1", "param2")}, 2000);
Try it Yourself »
Browser Support
setInterval()
is supported in all browsers:
Contact Sales
If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]
Report Error
If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use
,
cookie and privacy policy
.
W3Schools is Powered by W3.CSS
.