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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roy Wang

    #1

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

    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.DomDo cument");
    this.xmlDoc.loa d(url);

    The code below is loading the xml file for NS7 and FireFox0.9.3:
    this.xmlDoc = document.implem entation.create Document("","do c",null);
    this.xmlDoc.loa d(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.rea dyState 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.xmlD oc.readyState);
    var today2 = new Date();
    var now2 = today2.getTime( );
    if((now2 - now) > 500 || (this.xmlDoc.re adyState == 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.rea dyState to check.

    Is this.xmlDoc.onl oad 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
  • Steve van Dongen

    #2
    Re: How to determine when loading an xml file is complete? using javascript

    roy.wangcs@gmai l.com (Roy Wang) wrote:
    [color=blue]
    >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.DomDo cument");
    > this.xmlDoc.loa d(url);
    >
    >The code below is loading the xml file for NS7 and FireFox0.9.3:
    > this.xmlDoc = document.implem entation.create Document("","do c",null);
    > this.xmlDoc.loa d(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.rea dyState 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.xmlD oc.readyState);
    > var today2 = new Date();
    > var now2 = today2.getTime( );
    > if((now2 - now) > 500 || (this.xmlDoc.re adyState == 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.rea dyState to check.
    >
    > Is this.xmlDoc.onl oad a good way to detect the completion of loading?
    > What if i have to time out the loading after 0.5 seconds?
    >
    >Any suggestion?[/color]

    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.DOMDocum ent");
    xmldoc.onreadys tatechange = CheckState;
    xmldoc.load(url );
    timerId = setTimeout("Abo rt()", 500);

    function CheckState()
    {
    var state = xmldoc.readySta te;
    if (state == 4)
    {
    clearTimeout(ti merId);
    // XML is loaded
    }
    }

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

    Comment

    • Roy Wang

      #3
      Re: How to determine when loading an xml file is complete? using javascript

      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.DOMDocum ent");
      xmldoc.onreadys tatechange = CheckState;
      xmldoc.async = false;
      timerId = setTimeout("Abo rt()", 500);
      xmldoc.load(url );

      function CheckState()
      {
      var state = xmldoc.readySta te;
      if (state == 4)
      {
      clearTimeout(ti merId);
      // XML is loaded
      }
      }

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

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

      Thanks

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Steve van Dongen

        #4
        Re: How to determine when loading an xml file is complete? using javascript

        Roy Wang <nospam@devdex. com> wrote:
        [color=blue]
        >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.DOMDocum ent");
        >xmldoc.onready statechange = CheckState;
        >xmldoc.async = false;
        >timerId = setTimeout("Abo rt()", 500);
        >xmldoc.load(ur l);
        >
        >function CheckState()
        >{
        >var state = xmldoc.readySta te;
        >if (state == 4)
        >{
        >clearTimeout(t imerId);
        >// XML is loaded
        >}
        >}
        >
        >function Abort()
        >{
        >if (xmldoc.readySt ate != 4)
        >xmldoc.abort() ;
        >}
        >
        >will the function block at "load(url)" and time out after 500
        >miliseconds.
        >Or is there anyway to do that?[/color]

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

        Steve

        Comment

        Working...