Click to See Complete Forum and Search --> : for loop inside a while loop


llamatron
09-20-2003, 03:16 PM
I have the following piece of code that i need to execute twice. For various reasons i cant use for i = 1 to 10 :

for (i=0; i<5;i++){
setTimeout("newWindow2.location.href='http://www.myurl.com/myasp.asp",i*1000);
}

So i thought I would put it inside a while loop like this :

var j=0;
while(j < 2) {
for (i=0; i<5;i++){
setTimeout("newWindow2.location.href='http://www.myurl.com/myasp.asp",i*1000);
}
j++;
}

But the for part still only executes 5 times. Am I right in assuming that the code will continue executing the while loop before the for loop has even finished???

Charles
09-20-2003, 03:55 PM
A couple of things...

1) You can nest "for" statements and it might be a bit easier to do so here.

2) "setTimeout" isn't modal, it doesn't stop the execution of the script until it acts. Your whole routine will run before the first setTimeout accomplishes its goal. You have two outer loops, the "setTimeout"s of each one doing the same thing at the same time. They're all being executed in pairs; it's just that you cannot tell.

Khalid Ali
09-20-2003, 05:07 PM
From looking at your code this is what I have understood.

you want to run a script that opens a new page in the child window name

newWindow2

and you want top open 2 sets 5 pages each???

I am pretty sure you need to use
setInterval("newWindow2.location.href='http://www.myurl.com/myasp.asp",i*1000); )

what you can do is create a function that opens the window and call that functionin the setInterval() instead of direct code to open the window,and then in the window you can have the counter variables incremented.