Waiting for object

H

Hansen

Hi!

I have a problem with an object not yet being initialized when I try to
access it. Hence I would like to wait for the object to be initialized. I
have the following code:

try
{
if(gridArray == null)
{
setTimeout("waitForGridArray()", 500);
}


for (var i = 0; i < gridArray.length; ++i)
.....
}
}
catch(e)
{
alert(e.message);
}

function waitForGridArray()
{
if(gridArray == null)
{
setTimeout("waitForGridArray()", 500);
alert("Waiting);
}
}

The problem is that even though I want to wait I get an "gridArray.length is
null or not an object" error, followed by the "Waiting" alert.
Why doesn't it wait until gridArray is ready and the continue? (Windows
Server 2003)

Best regards
Hansen
 
R

RobG

Hansen said:
Hi!

I have a problem with an object not yet being initialized when I try to
access it. Hence I would like to wait for the object to be initialized. I
have the following code:

Please indent code, preferably using 2 or 4 spaces:
try
{
if(gridArray == null)
{
setTimeout("waitForGridArray()", 500);
}

The whole try..catch block is in a global scope and will be executed as
the script engine comes across it. Therefore after doing the 'if'
statement, whatever the result, it will continue to the following 'for'
statement. Put the try..catch inside a function and put a return inside
the 'if' block to stop execution.

for (var i = 0; i < gridArray.length; ++i)

{ // I guess there was an opening brace here?
....
}
}

catch(e)
{
alert(e.message);
}

function waitForGridArray()
{
if(gridArray == null)
{
setTimeout("waitForGridArray()", 500);
alert("Waiting);

Put a return here.


And call the function you want to execute here.

}

The problem is that even though I want to wait I get an "gridArray.length is
null or not an object" error, followed by the "Waiting" alert.
Why doesn't it wait until gridArray is ready and the continue? (Windows
Server 2003)

Because you continue with the code regardless of what the if block
returns. The following is a pattern that may work, however there is
likely a better way if you explain a little more about what you are
trying to do:

function doSomethingToGridArray()
{
/* ... */
}

function waitForGridArray()
{
if (typeof gridArray == 'undefined'){
setTimeout('waitForGridArray();', 500);
return;
}
doSomethingToGridArray();
}

waitForGridArray();
 
H

Hansen

Please indent code, preferably using 2 or 4 spaces:

Sorry - will do.

I made a small error not displaying the check as being inside a function. So
here is the actual code, with a few modifications.

function makeHTML()
{
try
{
if(gridArray == null)
{
waitForGridArray();
}
for (var i = 0; i < gridArray.length; ++i)
{
.... <do stuff>
}
}
catch(e)
{
alert(e.message);
}
}

function waitForGridArray()
{
if(gridArray == null)
{
setTimeout("waitForGridArray()", 500);
alert("Waiting);
}
else
{
return;
}
}

It seemed as if the setTimeout("waitForGridArray()", 500); I had in my if()
check, allowed the execution to continue, rendering the gridArray.length =
null error.
 
H

Hansen

It seems that I have misunderstud the use of setTimeout().
What I'm trying to do is to use it as a sleep, but the way I havde descriped
here, allows for futher execution, which causes the gridArray error.
I'm only saved by the alert() since it halts execution. But the alert is for
testing purpose so I need to find another solution.

How do I make a "sleep" that doesn't continue, until the gridArray object
exists?
 
R

RobG

Hansen said:
It seems that I have misunderstud the use of setTimeout().
What I'm trying to do is to use it as a sleep, but the way I havde descriped
here, allows for futher execution, which causes the gridArray error.
I'm only saved by the alert() since it halts execution. But the alert is for
testing purpose so I need to find another solution.

How do I make a "sleep" that doesn't continue, until the gridArray object
exists?

You can't, which is why I suggested having a 'wait' function that uses
setTimeout to call itself. When gridArray becomes available, then it
calls another function. You should not be using try..catch at all for this.
 
H

Hansen

RobG said:
You can't, which is why I suggested having a 'wait' function that uses
setTimeout to call itself. When gridArray becomes available, then it
calls another function. You should not be using try..catch at all for
this.

Thanks for the help. As you suggested, I've made a function which calls
itself until gridArray is ready, and then it calls makeHTML();

/Hansen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top