I have this Javascript code.
var headlineStore = [
function headline1(){
//irrelevant code
},
function headline2(){
//irrelevant code
},
function headline4(){
//irrelevant code
},
function headline4(){
//irrelevant code
},
function headline5(){
//irrelevant code
}
]
for(var i = 0; i < headlineStore.length; i++){ //for loop to loop through array named headlineStore
if(i == 4) //if condition to reset the counter once it reaches 5
{
i = 0;
}
(function(i){
setTimeout(function(){
headlineStore[i]();
}, 5000 * i);
}(i)); //Will load each function in the array with timeout increments
}
What I have here is a for loop that loops through an array filled with functions. On every iteration a function from the array is retrieved and executed with time intervals.
What I want is that after the last function is retrieved it loops through the array again starting with the first function and will do this infinitely.
What I tried is resetting the counter when it reaches 4 but it goes out of the loop and continue executing, then for some reason the page become unresponsive.