How to determine when loading an xml file is complete? using javascript

R

Roy Wang

hi,
My problem is how to determining when the XML file has loaded using
javascript.

I loaded an xml file using javascript in a web page.

The code below is loading the xml file for IE:
this.xmlDoc= new ActiveXObject("Microsoft.DomDocument");
this.xmlDoc.load(url);

The code below is loading the xml file for NS7 and FireFox0.9.3:
this.xmlDoc = document.implementation.createDocument("","doc",null);
this.xmlDoc.load(url);

I want to make sure the xml file is loaded successfully, or to time
out at 0.5 seconds.

problems in IE:
I use a while loop to check this.xmlDoc.readyState after the load,
I didn't set the xmlDoc.asnyc to false since i want to time out if
loading is very time-consuming.

var today = new Date();
var now = today.getTime();
while(1)
{
//alert(this.xmlDoc.readyState);
var today2 = new Date();
var now2 = today2.getTime();
if((now2 - now) > 500 || (this.xmlDoc.readyState == 4))
{
alert(now2 - now);
return;
}
}
However, I get timed-out every time! Even I change 500 to 5000, IE
will hang for 5 seconds and time-out.There is only a small xml file to
load. It shouldn't take that long!

Is it because dom loads the xml asynchronously and the while loop
takes higher priority than the loading thread?Or is there any way i
can manipulate the multi-thread behavior?
If i use synchronous loading, how can I wait for a while in the func
before return and time-out the loading when necessary?

problems in NS, FireFox:
there is no property such as this.xmlDoc.readyState to check.

Is this.xmlDoc.onload a good way to detect the completion of loading?
What if i have to time out the loading after 0.5 seconds?

Any suggestion?

Thanks,
Roy
 
S

Steve van Dongen

hi,
My problem is how to determining when the XML file has loaded using
javascript.

I loaded an xml file using javascript in a web page.

The code below is loading the xml file for IE:
this.xmlDoc= new ActiveXObject("Microsoft.DomDocument");
this.xmlDoc.load(url);

The code below is loading the xml file for NS7 and FireFox0.9.3:
this.xmlDoc = document.implementation.createDocument("","doc",null);
this.xmlDoc.load(url);

I want to make sure the xml file is loaded successfully, or to time
out at 0.5 seconds.

problems in IE:
I use a while loop to check this.xmlDoc.readyState after the load,
I didn't set the xmlDoc.asnyc to false since i want to time out if
loading is very time-consuming.

var today = new Date();
var now = today.getTime();
while(1)
{
//alert(this.xmlDoc.readyState);
var today2 = new Date();
var now2 = today2.getTime();
if((now2 - now) > 500 || (this.xmlDoc.readyState == 4))
{
alert(now2 - now);
return;
}
}
However, I get timed-out every time! Even I change 500 to 5000, IE
will hang for 5 seconds and time-out.There is only a small xml file to
load. It shouldn't take that long!

Is it because dom loads the xml asynchronously and the while loop
takes higher priority than the loading thread?Or is there any way i
can manipulate the multi-thread behavior?
If i use synchronous loading, how can I wait for a while in the func
before return and time-out the loading when necessary?

problems in NS, FireFox:
there is no property such as this.xmlDoc.readyState to check.

Is this.xmlDoc.onload a good way to detect the completion of loading?
What if i have to time out the loading after 0.5 seconds?

Any suggestion?

Don't use a busy loop to monitor the download. There's no such thing
as multi-threading in Javascript; if something is being processed then
nothing else is including the browser because it calls into the script
engine all the time to fire events. Implement your timeout using
setTimeout/clearTimeout.

xmldoc = new ActiveXObject("Msxml2.DOMDocument");
xmldoc.onreadystatechange = CheckState;
xmldoc.load(url);
timerId = setTimeout("Abort()", 500);

function CheckState()
{
var state = xmldoc.readyState;
if (state == 4)
{
clearTimeout(timerId);
// XML is loaded
}
}

function Abort()
{
if (xmldoc.readyState != 4)
xmldoc.abort();
}
 
R

Roy Wang

Hi, Steve,
Thanks for your reply.
However, the reason i use a while loop is that i want to stay in the
function for 0.5 seconds unless the readyState is set to 4.
And I understand there is no such way like "sleep()" in javascript to do
that.

what if i use synchronous loading like this:

xmldoc = new ActiveXObject("Msxml2.DOMDocument");
xmldoc.onreadystatechange = CheckState;
xmldoc.async = false;
timerId = setTimeout("Abort()", 500);
xmldoc.load(url);

function CheckState()
{
var state = xmldoc.readyState;
if (state == 4)
{
clearTimeout(timerId);
// XML is loaded
}
}

function Abort()
{
if (xmldoc.readyState != 4)
xmldoc.abort();
}

will the function block at "load(url)" and time out after 500
miliseconds.
Or is there anyway to do that?

Thanks
 
S

Steve van Dongen

Roy Wang said:
Hi, Steve,
Thanks for your reply.
However, the reason i use a while loop is that i want to stay in the
function for 0.5 seconds unless the readyState is set to 4.
And I understand there is no such way like "sleep()" in javascript to do
that.

what if i use synchronous loading like this:

xmldoc = new ActiveXObject("Msxml2.DOMDocument");
xmldoc.onreadystatechange = CheckState;
xmldoc.async = false;
timerId = setTimeout("Abort()", 500);
xmldoc.load(url);

function CheckState()
{
var state = xmldoc.readyState;
if (state == 4)
{
clearTimeout(timerId);
// XML is loaded
}
}

function Abort()
{
if (xmldoc.readyState != 4)
xmldoc.abort();
}

will the function block at "load(url)" and time out after 500
miliseconds.
Or is there anyway to do that?

No and no, you have to do it like I outlined. Why do you want to stay
in the function?

Steve
 

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

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,242
Latest member
KendrickKo

Latest Threads

Top