Interval in JavaScript

Marc Wagner, July 22, 2022

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.

Avatar of Marc Wagner
Marc Wagner

Hi Marc here. I'm the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.

Similar Topics

Comments

Leave A Comment

Title