473,395 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Waiting for object

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
5 4414
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
> 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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Burns | last post by:
Hi, I'm writing some code that talks to a POP3 server with a TcpClient. I can send and receive using a NetworkStream, but when I send my message, then try to recieve, no response has returned. ...
7
by: gerry | last post by:
I have a com object that is used via an interop assembly - ie. I referenced the com object and vs.net created the interop .dll for it. I create one object per session when the page 1st loads and...
2
by: Anson | last post by:
Hello, I would like to clarify something about Remoting Object in .NET. Knowing from MSDN that in order for Remote Object to work, I need 3 components. a) RemotableType object, just like a...
12
by: Raymond Lewallen | last post by:
How to wait for a process to stop completion is my goal. Obviously, the looping while waiting for the HasExited property is not a solution.. but thats the best I can come up off the top of my...
16
by: Bruce Wood | last post by:
Maybe it's just late in my day, but I'm reading Jon's article on threading, in particular how to use Monitor.Wait() and Monitor.Pulse(), and there's something that's not sinking in. The code in...
2
by: Bob | last post by:
I launch Word from within my application (Vs2005, VB) and I would like to make it necessary for the just opened Word to be closed before the rest of my code in the calling sub can execute. How can...
0
by: =?Utf-8?B?QmlsbEI=?= | last post by:
This is a tough one... My Windows Service app periodically performs some processing on new entries to a SQL Server database table. It uses a simple System.Timer to call the Elapsed event...
1
by: Simon_21 | last post by:
Hi All, I have a servlet which is invoked from a jsp page. While the serlvet is executing, the jsp page is waiting for the servlet to complete. When the servlet completes, it informs the waiting...
3
by: Sarah | last post by:
Hi - Please be gentle. I am quite new to visual basic, but I have been going through tutorials and reading up. I found a code snippet on the internet that I wanted to see if I could re-purpose...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.