Definition and Use
The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).
The setInterval() method will continue calling the function until clearInterval() is called or the window is closed.
The ID value returned by setInterval() is used as parameter for the clearInterval method ().
Tip: 1000 ms = 1 second.
Tip: To perform a function only once, after a specified number of milliseconds, use the setTimeout method () .
var myVar;
Function myFunction() {
myVar = setInterval(alertFunc, 3000);
}
Function alertFunc() {
Alert("Hello!");
}
(the setInterval() method will perform the function once every 1 second, like a digital clock):
var myVar = setInterval(myTimer, 1000);
Function myTimer() {
var d = new Date();
var t = d.toLocaleTimeString();
Document.getElementById("demo"). innerHTML = t;
}