I have the following code :
var i = 0, count = 10, randomId;
function f(myArray) {
// this will get a random value from myArray
randomId = myArray[Math.floor( Math.random()*myArray.length )];
// displays the random value
alert (randomId);
i++;
if( i < count ){
setTimeout( f, 3000 );
}
}
f(myArray);
The above code works but gives only one alert and then it stops.
However it works properly (10 loops) with basic alerts such as alert("hi"), and remove the randomId line.
It's as if anything complex within this function will block the loop, it will only handle basic alerts..
Any help is appreciated, thanks :)
f(myArray), but where ismyArray? Your recursive call doesn't pass anything tof, so it will beundefined, thus creating an error when you domyArray.lengthsetTimeout(f, 3000, myArray)setTimeout(function() { f(myArray) }, 3000)