Delayed 'for loop' in javascript -


i have delayed loop working displays numbers 0-3 1 second delay, want start process on again.

i tried putting whole code while loop x<99 (something never occur therefore making loop repeat forever) doesn't seem working

here code:

sequence=["0","1","2","3"];  while (x<99) { x=-1; (function myloop (i) {              settimeout(function () {       x++;    document.write(sequence[x] + "<br/>");    if (--i) myloop(i);     }, 1000)   })(4);  } 

here code-pen link

can please help?

thanks

a loop won't work in case. there multiple ways this. 1 call settimeout "recursively", increasing index @ each call.

var sequence=["0","1","2","3"];  (function() { // boilerplate code keep `i` private     var = 0; // running index     (function run() {         console.log(sequence[i]);         = (i+1) % sequence.length; // increase `i` , wrap around         settimeout(run, 1000); // next iteration     }()); }()); 

Comments