title: Javascript setTimeout with function in a for loop
body: <p>Using Javascript, I'm trying to loop through an array and execute a function with a time delay on each loop. This does not work:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  movesArray = new Array("s","s","s","s","s","s","s","s","s","s","s","s","s","s");
  var pause = 100;
  for (i=0; i&lt;=14; i++)
  {
      var t=setTimeout("ProcessKeypress(movesArray[i])", pause);
      pause = pause+100;
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>However, if I just repeat it multiple times it does work:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  var t=setTimeout("ProcessKeypress('s')", 100);
  var t=setTimeout("ProcessKeypress('s')", 200);
  var t=setTimeout("ProcessKeypress('s')", 300);
  var t=setTimeout("ProcessKeypress('s')", 400);
  var t=setTimeout("ProcessKeypress('s')", 500);
  
  &lt;/script&gt;
  </code></pre>
  
  <p>The problem is that in some cases I will need to repeat the function a few hundred times (maybe more) which creates a large webpage. Is there a way to loop through the array and run the function on a time delay?</p>