By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,452 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,452 IT Pros & Developers. It's quick & easy.

Waiting for object

P: n/a
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
Jun 27 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hansen wrote:
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();

--
Rob
Jun 27 '06 #2

P: n/a
> 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.

Jun 27 '06 #3

P: n/a
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?
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); //Execution
continues for another 500 ms.
alert("Waiting);
//Saves me, since othervise I would have returned and accessed the
gridArray object
}
else
{
return;
}
}

Jun 27 '06 #4

P: n/a
Hansen wrote:
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.
--
Rob
Jun 27 '06 #5

P: n/a

"RobG" <rg***@iinet.net.au> wrote in message
news:44**********************@per-qv1-newsreader-01.iinet.net.au...
Hansen wrote:
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.


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
Jun 27 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.