Click to See Complete Forum and Search --> : wait until existence of a variable from a loading page


cyrrhose
11-17-2003, 11:09 AM
Hello,
I'd to delay execution of a function until existence of a variable from a loading frame. For example:

...
cpt=0;
while (myFrame.getMyVariable()==null || cpt<10){
window.setTimeout("cpt++";100);
}

...


}
but this doesn't seem to work and leeds to an infinite loop.

Can someone please help?
Thanks

Gollum
11-18-2003, 05:19 AM
No not a silly question:)

Your code is executing an infinite loop because the code in your timeout handler isn't getting a chance to execute as your while loop never ends - Javascript is not pre-emptive like that.

what you want is to put your variable test in the timeout handler, something like this...

function checkVariable()
{
if ( myFrame.getMyVariable() != null )
{
myFrame.doSomethingThen();
}
else
{
window.setTimeout("checkVariable();",100);
}
}

checkVariable();


The code above will itterate indefinitely until the test evaluates true, but as it is resting 100ms between times you won't notice it much.

You could put a counter in and stop after a number if you like, but I'll leave that to you ;)

Charles
11-18-2003, 06:02 AM
A better solution would be to use the window.onload handler of the frame with the variable to call a function in the other frame. Just make sure everything fails safe when the documents are separated from the frameset and when there is no JavaScript.

cyrrhose
11-18-2003, 07:51 AM
This Helps!
Thank you very much guys!