Interval in JavaScript

Marc Wag­ner

Janu­ary 26, 2022

1 min read|

Java­Script allows you to call a script on a regu­lar basis. The set­In­ter­val func­tion is available for this pur­po­se.

To create an interval in JavaScript with setInterval #

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

Alter­na­tively, you can of cour­se pass func­tions ins­tead 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 cour­se you can stop an inter­val at any time, for this you can use clear­In­ter­val.

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

clearInterval(myInterval);

To extend the interval by parameters #

Fur­ther­mo­re, the who­le thing can also be exten­ded with para­me­ters:

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

Other para­me­ters can be added in the same sche­me.

88e86fcb816eff22bc917094df2862d8dd5c0e978b333e6dd5f36f808990c261 96

Arti­kel von:

Marc Wag­ner

Hi Marc here. I’m the foun­der of Forge12 Inter­ac­ti­ve and have been pas­sio­na­te about buil­ding web­sites, online stores, appli­ca­ti­ons and SaaS solu­ti­ons for busi­nesses for over 20 years. Befo­re foun­ding the com­pa­ny, I alre­a­dy work­ed in publicly lis­ted com­pa­nies and acqui­red all kinds of know­ledge. Now I want to pass this know­ledge on to my cus­to­mers.

Hast du eine Fra­ge? Hin­ter­lass bit­te einen Kom­men­tar