0

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:

<script type="text/javascript">
movesArray = new Array("s","s","s","s","s","s","s","s","s","s","s","s","s","s");
var pause = 100;
for (i=0; i<=14; i++)
{
    var t=setTimeout("ProcessKeypress(movesArray[i])", pause);
    pause = pause+100;
}
</script>

However, if I just repeat it multiple times it does work:

<script type="text/javascript">

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);

</script>

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?

Marcus
  • 4,400
  • 13
  • 48
  • 64
  • 2
    There is so much wrong with this I don't even know where to start.... – Justin Thomas Dec 09 '11 at 00:28
  • 1
    You should never pass a string to setTimeout... – ThiefMaster Dec 09 '11 at 00:29
  • It may not be the problem, but you are wrong to add single quotes in the firs parameter to setTimeout. Try `var t=setTimeout("ProcessKeypress(movesArray[i])", pause);` instead. – Borodin Dec 09 '11 at 00:31
  • 1
    Why the -1? I may be doing a lot of things wrong with my code; but that's why I asked the question here to get input on what I should change. – Marcus Dec 09 '11 at 00:43

3 Answers3

2

You are passing the string "movesArray[i]". Just escape the string

movesArray = new Array("s","s","s","s","s","s","s","s","s","s","s","s","s","s");
var pause = 100;
for (i=0; i<=14; i++)
{
    var t=setTimeout("ProcessKeypress(movesArray[" +i +"])", pause);
    pause = pause+100;
}
jermel
  • 2,326
  • 21
  • 19
1

Use setInterval(). You can run a function at any interval:

setInterval(function(){
    // do stuff
}, 300);

Also, use function(){} inside the setTiemout instead of just the code:

setTimeout(function(){
    // do stuff
}, pause);
Purag
  • 16,941
  • 4
  • 54
  • 75
1

If you want each iteration of the loop to go a certain time delay after the previous one finishes, then your way of doing it will have an issues with the index into the array. These are the issues I see:

  1. Your index will not be correct as all calls to ProcessKeypress() will have i == 15.
  2. It's also better to pass setTimeout() an actual javascript function rather than a string that it has to evaluate.
  3. And, I also think it's generally better to just set the next timer when the first completes rather than set all of them at once (though either can work).
  4. And, your code was going up to movesArray[14], but you didn't have that many items in the array. It's better to refer to the array .length rather than hard code the 14.

My suggestions can be implemented like this:

<script type="text/javascript">
movesArray = new Array("s","s","s","s","s","s","s","s","s","s","s","s","s","s");
var i = 0;

function nextIteration() {
    ProcessKeypress(movesArray[i++]);
    if (i < movesArray.length) {
        setTimeout(nextIteration, 100);
    }
}

nextIteration();

</script>
jfriend00
  • 683,504
  • 96
  • 985
  • 979