JavaScript setInterval() Method | Virtual Species

JavaScript setInterval() Method

JavaScript setInterval() method either call a function evaluates an expression at specified number of milliseconds. This method continues to call/evaluate the expression until window is called. Another way to stop this method is by calling clearInterval() method. Below is a working demo of this useful method.

Hit start..


     This method is very handy on requirement of executing an action over over again. The below example script illustrates the setInterval() method. It takes a function with one second delay, in this case its 1000 millisecond. 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var dateId4AutoGen;
 
function autoGenerateDt() {
    var dateAuto = new Date();
    document.getElementById("datePnlAuto").innerHTML = dateAuto;
}
 
function autoDtSrt() {
    if (dateId4AutoGen) {
        clearInterval(dateId4AutoGen);
    }
    dateId4AutoGen = setInterval(function () {
        autoGenerateDt();
    }, 1000);
};

No comments:

Post a Comment