Definition:
When you want the code you have written to run repeatedly at a set time, you use this function in your program.
If you set the time, this function will run the code repeatedly on its own. You do not need to run the program manually.
Now you will clearly understand with the help of the flowchart.
Start | v Write your code insidesetInterval()| v Set the time interval | v Run thesetInterval()function | v Code executes | v Wait for the time interval
Syntax:
setInterval(function() {
// This code will run repeatedly after the given time.
}, time_in_milliseconds);
Example:
setInterval(function() {
console.log("Hello!");
}, 2000);
Output:
Hello! Hello! Hello! ...(keeps repeating every 2 seconds)
