Javascript loop through array infinitely not working
<p>I have this Javascript code. </p>

<pre class="lang-js prettyprint-override"><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 &lt; 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 
 }  
</code></pre>

<p>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. </p>

<p>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. </p>

<p>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. </p>