Click to See Complete Forum and Search --> : Loop while null or undefined.


gt37
10-04-2005, 04:20 AM
Hi,
I have another web application that opens in a window and runs a query in it after some simple timeout functions. that works ok but I d like to know at once when it is ready, not an estimated (about 10seconds settimeout()) depending on the clientcomputers cpu.
The question is HOW would I in the best way execute these functions without settimeout, instead use a loop that goes on until all the variables is defined in that application?
The problem is that the variable okToSend in the opened application "is null or not an object" (errormessage) :mad: when I try to check it as below.
Any ideas?
Thanks!//Martin

function imsFunct(){
mapWindow.focus();
var i = 0;
do
{
i+=1;
}
while(((mapWindow.MapFrame.okToSend=="undefined")||(mapWindow.MapFrame.okToSend=="null")||(!mapWindow.MapFrame.okToSend)));
setActive();
do
{
i+=1;
}
while(((mapWindow.MapFrame.okToSend==undefined)||(mapWindow.MapFrame.okToSend==null)||(!mapWindow.Ma pFrame.okToSend)));
findHit();
}

Kor
10-04-2005, 04:56 AM
The problem is that the variable okToSend


I quite don't get what you want to do, but there is an object, not a variable down there. Anyway, I guess that you should use the typeof operator to checked whether the type of the object/variable is undefined or not

gt37
10-04-2005, 08:15 AM
the variable (okToSend) is a variable declared inside MapFrame that I use to check when the app is ready for a new request. its true or false.

Anyhow When I try to check the value of it, its not defined at all, since I just open the application in a new window.
So I get the "mapWindow.MapFrame.okToSend" is null or not an object" as soon as I try to check it.

The IFs for undefined or null should have taken care of this but somehow it does not work. Tried with or without quotes.


How could I use typeof condition? sounds interesting

herodote92
10-04-2005, 08:41 AM
I guess the child window would have to set a parent-window variable to 'true' when it's ready. This variable would have been set previously to 'false' by the parent window itself, so it would never be undefined.

Well, that's just a general idea, I might be wrong...

gt37
10-04-2005, 09:34 AM
It worked with a variable in the opener set to true by the window.!
BUT...I get the " a script on this page make internetexplorer run slow" do you want to abort the script? If you dont the computer might hang up.
How could I do a loop without this alert popping up?

herodote92
10-04-2005, 11:36 AM
Every time I got such a message it came out that there was a bug of mine somewhere.

herodote92
10-04-2005, 11:44 AM
Oh, I hope you used a timeout for your loop ? Because if you just test the flag continually without breathing, the message you got would be quite justified. You should test it every, say, 100 or 500 milliseconds, or so.

gt37
10-05-2005, 07:27 AM
No sorry to say I did not use a timeout or interval
How could I achive that?

I did like below:

function imsFunct1(){
var i = 0;
mapWindow.focus();
do {i=i+1 }
while(!loaded);
setActive();
setTimeout("findHit();",500);
}

herodote92
10-05-2005, 07:57 AM
I made a small example here. Clicking the button won't change directly the message: it will only set the flag to TRUE. The background routine checks this flag every 2 secunds, when it finds it's TRUE, it changes the message. That's why there can be a delay between clicking and seeing the message changing. The background routine shouldn't be much time-consuming.


<html>
<head>
<title>Getting flag from somewhere else</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
var my_flag = false;
var t = "";
var ctr = 1;

function TestIt() {
t = setInterval("STestIt();",2000);
}
function STestIt() {
if (my_flag) {
clearInterval(t);
var msg = "Hurrah! Flag value is TRUE !<br>I checked for it "+ctr+" time(s) !"
document.getElementById("my_text").innerHTML = msg;
}
else ctr++;
}
</script>
</head>
<body onload = "TestIt();">
<input name="" type="button" value="Click" onclick="my_flag=true;">
<br><br>
<div id="my_text">
Alas, Flag value is still FALSE...
</div>
</body>
</html>

gt37
10-05-2005, 11:06 AM
Thanks alot KOR and HERODOTE92, this last bit was very handy to learn!
It now works great and runs the code quick as it is possible. :D

The results became two checks of this "loop" type of check
var t1="";
var varv = 1;
function checkOkToSend(){
t1=setInterval("chkOKTS();",500);
}
function chkOKTS(){
mapWindow.focus();
if(''+mapWindow.MapFrame.okToSend+''=="true"){
clearInterval(t1);
setActive();
findHit();
}
else varv++;
}
var t = "";
var ctr = 1;
function imsFunct() {
t = setInterval("STestIt();",500);
}
function STestIt() {
if (loaded) {
clearInterval(t);
mapWindow.focus();
//alert("loaded");
checkOkToSend();
}
else ctr++;
}