473,667 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("wai tForGridArray() ", 500);
}
for (var i = 0; i < gridArray.lengt h; ++i)
.....
}
}
catch(e)
{
alert(e.message );
}

function waitForGridArra y()
{
if(gridArray == null)
{
setTimeout("wai tForGridArray() ", 500);
alert("Waiting) ;
}
}

The problem is that even though I want to wait I get an "gridArray.leng th 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 4429
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("wai tForGridArray() ", 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.lengt h; ++i)
{ // I guess there was an opening brace here?
....
}
}
catch(e)
{
alert(e.message );
}

function waitForGridArra y()
{
if(gridArray == null)
{
setTimeout("wai tForGridArray() ", 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.leng th 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 doSomethingToGr idArray()
{
/* ... */
}

function waitForGridArra y()
{
if (typeof gridArray == 'undefined'){
setTimeout('wai tForGridArray() ;', 500);
return;
}
doSomethingToGr idArray();
}

waitForGridArra y();

--
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)
{
waitForGridArra y();
}
for (var i = 0; i < gridArray.lengt h; ++i)
{
.... <do stuff>
}
}
catch(e)
{
alert(e.message );
}
}

function waitForGridArra y()
{
if(gridArray == null)
{
setTimeout("wai tForGridArray() ", 500);
alert("Waiting) ;
}
else
{
return;
}
}

It seemed as if the setTimeout("wai tForGridArray() ", 500); I had in my if()
check, allowed the execution to continue, rendering the gridArray.lengt h =
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)
{
waitForGridArra y();
}
for (var i = 0; i < gridArray.lengt h; ++i)
{
.... <do stuff>
}
}
catch(e)
{
alert(e.message );
}
}

function waitForGridArra y()
{
if(gridArray == null)
{
setTimeout("wai tForGridArray() ", 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.ne t.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
2006
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. I'm assuming that this is because the server hasn't had the chance to respond before the recieve buffer is read. If this is the case, then how do I tell my code to wait around before checking the stream? If I just have a do{}until loop constantly...
7
1564
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 store it in the Session. I expected that the object would be released when the Session object was destroyed during at Session_End(). In my log I can see the Session_End() event firing as it should after the specified idle time, however the actual...
2
1319
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 regular dll with additional remotable feature. b) Host Application, which is a listener listens for remote clients of a remotable type
12
2364
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 head. Natuarally it will not work. I expect it to use up all resources looping, which will not allow the process to complete. The process takes about 60 seconds, because the .bat file it is calling is rebuilding mulitple .NET solutions and projects...
16
1952
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 question looks like this: public class ProducerConsumer { readonly object listLock = new object(); Queue queue = new Queue();
2
1770
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 I do this? Thanks for any help Bob
0
1849
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 handler (which contains the processing code) every minute. The processing itself usually takes no more than a few seconds. Even so, I have a boolean set in the event handler when I'm currently processing information so that only one thread is...
1
2766
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 jsp to redirect to another page. The waiting jsp has implemented onload which will redirect to the page informed by the servlet Server : (tomcat 6.0), Browser - IE 6.0, OS - WinXP
3
2324
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 for a project, but I keep getting the error: "Object reference not set to an instance of an object" for the 7th line of the code below which is: obj.ConvertPage(URL).Save("C:\screencaptest2.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
0
8367
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8570
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8650
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.