Interval in JavaScript

JavaScript allows you to call a script on a regular basis. The setInterval function is available for this purpose.

To create an interval in JavaScript with setInterval #

setInterval(function(){ 
     console.log("Ich werde jede Sekunde aufgerufen."); 
}, 1000);

Alternatively, you can of course pass functions instead of using inline code.

function debugHello(){
    console.log("Ich werde jede Sekunde aufgerufen.");
}

setInterval(debugHello, 1000);

To remove/stop an interval after you have set it #

Of course you can stop an interval at any time, for this you can use clearInterval.

var myInterval = setInterval(function(){ 
      console.log("Ich werde jede Sekunde aufgerufen."); 
}, 1000);

clearInterval(myInterval);

To extend the interval by parameters #

Furthermore, the whole thing can also be extended with parameters:

var myInterval = setInterval(function(seconds){ 
       console.log("Ich werde jede "+seconds+" Sekunde aufgerufen");
}, 1000, "1");

Other parameters can be added in the same scheme.

Hier klicken, um den Beitrag zu bewerten
[Gesamt: 0 Durchschnitt: 0]

Leave A Comment

Title