0

I am trying to execute a for every certain time to use it in javascript F12 console. How do i do that?

setTimeout(function(){
     window.location.reload(1); //used for something
     for (i = 0; i < 10; i++) {
         /*some code (I dont want this part to execute all at once but every certain 
         time so i can see what is going on)*/
     }
}, 500);
E Alexis T
  • 100
  • 9
  • Your setTimeout will wait 1/2 second then execute the call back function. How about just remove the for loop? if the reload() function is doing something and you want to monitor that then add another inner setTimeout () after it. – Leng Nov 23 '15 at 17:27
  • 1
    When you reload the page, all the scripts stop.' – Barmar Nov 23 '15 at 17:34
  • If you want to run something every time period, use `setInterval`, not `setTimeout`. – Barmar Nov 23 '15 at 17:34
  • @Barmar yes, that is what i want. So, there is no way of reloading the page and seeing the value of "i" increase in the for loop with alert(i) since the script stops? – E Alexis T Nov 23 '15 at 18:26
  • Save `i` in a cookie or localStorage. When the page loads, get the value out of the cookie and increment it. – Barmar Nov 23 '15 at 19:59
  • @Barmar Thank you, that is what I was looking for. – E Alexis T Nov 24 '15 at 13:22

1 Answers1

-1

If you really want to go with for loop then I guess what you are looking for is this

for (var i = 0; i <10 ; i++){
setTimeout(function(){
console.log("test :" +i);
},500*i);
}

or you should use setInterval instead of setTimeout

Arif
  • 1,617
  • 9
  • 18
  • This will log `test: 10` all the times. See http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example – Barmar Nov 24 '15 at 15:23