I'm unsure of why the following lines won't behave as intended:
[code]
function loop() {
var colors = ["red", "green", "blue"];
for(var x=0;x<colors.length;x++) {
setTimeout("//change the bgcolor of some element to colors[x]", 500);
}
} loop();
[/code]
The timeout function for whatever peculiar reason [i](or for my limited scope of knowledge pertaining to the language (JavaScript))[/i] won't delay each iteration of the loop.
From my understanding, [i](and supposing that we had the first argument of the timeout function change the bg color of some HTML element)[/i] the function should delay each execution by 500 milliseconds until x has reached the length of colors [i](2)[/i], although do feel free to correct me if I'm wrong [i](note: I'm not searching for alternative solutions, I'm just curious of why this won't work)[/i].
wrong forum
[editline]18th December 2010[/editline]
Plus, setTimeout() just takes a function and a timeout in milliseconds and schedules that function to be called when that timeout expires.
It doesn't pause execution of your script.
[QUOTE=Siemens;26772187]wrong forum
[editline]18th December 2010[/editline]
Plus, setTimeout() just takes a function and a timeout in milliseconds and schedules that function to be called when that timeout expires.
It doesn't pause execution of your script.[/QUOTE]
Ahh, thanks! :D
Apologies for the bump, but I'm still having problems executing iteration delays via this means (e.g. to delay the change of a property by x milliseconds via the setTimeout function). If I use the for loop as illustrated above, it'll automatically default the response to the last iteration (2 ("blue")) rather than to proceed through each with a 500ms delay before shifting to the next iteration.
Sorry, you need to Log In to post a reply to this thread.