XMLHttpRequest Object: Asynchronous vs Synchronous Mode

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

    XMLHttpRequest Object: Asynchronous vs Synchronous Mode

    Hi All,

    I've got a question about Asynchronous vs Synchronous mode with the
    XMLHttpRequest object. What are the ramifications of using one mode
    vs the other? If the script uses Asynchronous mode, it sounds as if a
    thread retrieves the data from the supplied URL and the JS function
    that called the open() and send() methods continues on. Where as
    using Synchronous mode the method that called open() and send() waits
    until the data from the URL has been retrieved. Am I correct in my
    thinking?

  • -Lost

    #2
    Re: XMLHttpRequest Object: Asynchronous vs Synchronous Mode

    "HugeBob" <rnurse@gmail.c omwrote in message
    news:1175177691 .794121.187500@ l77g2000hsb.goo glegroups.com.. .
    Hi All,
    >
    I've got a question about Asynchronous vs Synchronous mode with the
    XMLHttpRequest object. What are the ramifications of using one mode
    vs the other? If the script uses Asynchronous mode, it sounds as if a
    thread retrieves the data from the supplied URL and the JS function
    that called the open() and send() methods continues on. Where as
    using Synchronous mode the method that called open() and send() waits
    until the data from the URL has been retrieved. Am I correct in my
    thinking?


    The ramifications of not using the asynchronous aspect of AJAX is having the UA or browser
    block activity until the request returns, finishes, loads, whatever.

    I would not say that the open() and send() methods continue. The onreadystatecha nge
    property still receives a dispatch however (even when blocking). So instead of being
    notified of what open() and send() are doing you will receive an onload event and the
    readyState of 4.

    I would say you are incorrect in thinking that it cannot update its interface during a
    synchronous call (assuming you really meant onreadystatecha nge instead of open() and
    send()).

    I hope I worded all that correctly.

    -Lost


    Comment

    • HugeBob

      #3
      Re: XMLHttpRequest Object: Asynchronous vs Synchronous Mode

      On Mar 29, 10:58 am, "-Lost" <missed-s...@comcast.ne twrote:
      "HugeBob" <rnu...@gmail.c omwrote in message
      >
      news:1175177691 .794121.187500@ l77g2000hsb.goo glegroups.com.. .
      >
      Hi All,
      >
      I've got a question about Asynchronous vs Synchronous mode with the
      XMLHttpRequest object. What are the ramifications of using one mode
      vs the other? If the script uses Asynchronous mode, it sounds as if a
      thread retrieves the data from the supplied URL and the JS function
      that called the open() and send() methods continues on. Where as
      using Synchronous mode the method that called open() and send() waits
      until the data from the URL has been retrieved. Am I correct in my
      thinking?
      >

      >
      The ramifications of not using the asynchronous aspect of AJAX is having the UA or browser
      block activity until the request returns, finishes, loads, whatever.
      >
      I would not say that the open() and send() methods continue. The onreadystatecha nge
      property still receives a dispatch however (even when blocking). So instead of being
      notified of what open() and send() are doing you will receive an onload event and the
      readyState of 4.
      >
      I would say you are incorrect in thinking that it cannot update its interface during a
      synchronous call (assuming you really meant onreadystatecha nge instead of open() and
      send()).
      >
      I hope I worded all that correctly.
      >
      -Lost
      I find myself in the same state as you signature, LOL. Well, sort
      of. I've got an app that uses AJAX. The onreadystatecha nge method
      sets a global variable with XML it retrieves from the server when
      readyState is 4. The global variable is later searched via JS. The
      application works in IE when synchronous mode is used and not at all
      in FireFox 1.5 (the onreadystatecha nge method never seems to run).
      So, take the following function:

      globalVariable = null;
      function ajaxFunction( )
      {
      var xmlHttp;

      xmlHttp = getXMLHTTPReque stObject( );
      alert(xmlHttp);
      xmlHttp.onready statechange=fun ction()
      {
      if(xmlHttp.read yState==4)
      {
      globalVariable = xmlHttp.respons eText;
      }
      }

      xmlHttp.open("G ET","time.asp", true);
      xmlHttp.send(nu ll);
      }

      So, are you saying that even when this function completes,
      globalVariable may not have anything in it when it's accessed later
      regardless of whether I use asynch or synch mode?

      Comment

      • -Lost

        #4
        Re: XMLHttpRequest Object: Asynchronous vs Synchronous Mode

        "HugeBob" <rnurse@gmail.c omwrote in message
        news:1175183260 .060842.183960@ d57g2000hsg.goo glegroups.com.. .
        On Mar 29, 10:58 am, "-Lost" <missed-s...@comcast.ne twrote:
        >"HugeBob" <rnu...@gmail.c omwrote in message
        >>
        >news:117517769 1.794121.187500 @l77g2000hsb.go oglegroups.com. ..
        >>
        Hi All,
        >>
        I've got a question about Asynchronous vs Synchronous mode with the
        XMLHttpRequest object. What are the ramifications of using one mode
        vs the other? If the script uses Asynchronous mode, it sounds as if a
        thread retrieves the data from the supplied URL and the JS function
        that called the open() and send() methods continues on. Where as
        using Synchronous mode the method that called open() and send() waits
        until the data from the URL has been retrieved. Am I correct in my
        thinking?
        >>
        >http://www.w3.org/TR/XMLHttpRequest/#dfn-send
        >>
        >The ramifications of not using the asynchronous aspect of AJAX is having the UA or
        >browser
        >block activity until the request returns, finishes, loads, whatever.
        >>
        >I would not say that the open() and send() methods continue. The onreadystatecha nge
        >property still receives a dispatch however (even when blocking). So instead of being
        >notified of what open() and send() are doing you will receive an onload event and the
        >readyState of 4.
        >>
        >I would say you are incorrect in thinking that it cannot update its interface during a
        >synchronous call (assuming you really meant onreadystatecha nge instead of open() and
        >send()).
        >>
        >I hope I worded all that correctly.
        >>
        >-Lost
        >
        I find myself in the same state as you signature, LOL. Well, sort
        of. I've got an app that uses AJAX. The onreadystatecha nge method
        sets a global variable with XML it retrieves from the server when
        readyState is 4. The global variable is later searched via JS. The
        application works in IE when synchronous mode is used and not at all
        in FireFox 1.5 (the onreadystatecha nge method never seems to run).
        So, take the following function:
        >
        globalVariable = null;
        function ajaxFunction( )
        {
        var xmlHttp;
        >
        xmlHttp = getXMLHTTPReque stObject( );
        alert(xmlHttp);
        xmlHttp.onready statechange=fun ction()
        {
        if(xmlHttp.read yState==4)
        {
        globalVariable = xmlHttp.respons eText;
        }
        }
        >
        xmlHttp.open("G ET","time.asp", true);
        xmlHttp.send(nu ll);
        }
        >
        So, are you saying that even when this function completes,
        globalVariable may not have anything in it when it's accessed later
        regardless of whether I use asynch or synch mode?
        No, globalVariable will have the responseText specified by the XMLHttpRequest. However,
        if asynchronous is set to false, depending on the size of the server's output (e.g. how
        long it takes to load) globalVariable will still have the responseText, it may not be
        available as quickly.

        Assuming of course that getXMLHTTPReque stObject() returns the proper XMLHttpRequest.

        To support the greatest number of possible browsers first have a look at:



        ....the implementation there is fairly solid.

        -Lost


        Comment

        • HugeBob

          #5
          Re: XMLHttpRequest Object: Asynchronous vs Synchronous Mode

          On Mar 29, 12:21 pm, "-Lost" <missed-s...@comcast.ne twrote:
          "HugeBob" <rnu...@gmail.c omwrote in message
          >
          news:1175183260 .060842.183960@ d57g2000hsg.goo glegroups.com.. .
          >
          >
          >
          On Mar 29, 10:58 am, "-Lost" <missed-s...@comcast.ne twrote:
          "HugeBob" <rnu...@gmail.c omwrote in message
          >
          >news:117517769 1.794121.187500 @l77g2000hsb.go oglegroups.com. ..
          >
          Hi All,
          >
          I've got a question about Asynchronous vs Synchronous mode with the
          XMLHttpRequest object. What are the ramifications of using one mode
          vs the other? If the script uses Asynchronous mode, it sounds as if a
          thread retrieves the data from the supplied URL and the JS function
          that called the open() and send() methods continues on. Where as
          using Synchronous mode the method that called open() and send() waits
          until the data from the URL has been retrieved. Am I correct in my
          thinking?
          >>
          The ramifications of not using the asynchronous aspect of AJAX is having the UA or
          browser
          block activity until the request returns, finishes, loads, whatever.
          >
          I would not say that the open() and send() methods continue. The onreadystatecha nge
          property still receives a dispatch however (even when blocking). So instead of being
          notified of what open() and send() are doing you will receive an onload event and the
          readyState of 4.
          >
          I would say you are incorrect in thinking that it cannot update its interface during a
          synchronous call (assuming you really meant onreadystatecha nge instead of open() and
          send()).
          >
          I hope I worded all that correctly.
          >
          -Lost
          >
          I find myself in the same state as you signature, LOL. Well, sort
          of. I've got an app that uses AJAX. The onreadystatecha nge method
          sets a global variable with XML it retrieves from the server when
          readyState is 4. The global variable is later searched via JS. The
          application works in IE when synchronous mode is used and not at all
          in FireFox 1.5 (the onreadystatecha nge method never seems to run).
          So, take the following function:
          >
          globalVariable = null;
          function ajaxFunction( )
          {
          var xmlHttp;
          >
          xmlHttp = getXMLHTTPReque stObject( );
          alert(xmlHttp);
          xmlHttp.onready statechange=fun ction()
          {
          if(xmlHttp.read yState==4)
          {
          globalVariable = xmlHttp.respons eText;
          }
          }
          >
          xmlHttp.open("G ET","time.asp", true);
          xmlHttp.send(nu ll);
          }
          >
          So, are you saying that even when this function completes,
          globalVariable may not have anything in it when it's accessed later
          regardless of whether I use asynch or synch mode?
          >
          No, globalVariable will have the responseText specified by the XMLHttpRequest. However,
          if asynchronous is set to false, depending on the size of the server's output (e.g. how
          long it takes to load) globalVariable will still have the responseText, it may not be
          available as quickly.
          >
          Assuming of course that getXMLHTTPReque stObject() returns the proper XMLHttpRequest.
          >
          To support the greatest number of possible browsers first have a look at:
          >

          >
          ...the implementation there is fairly solid.
          >
          -Lost
          Oh, my getXMLHTTPReque stObject() method handles that. I borrowed it
          from W3Schools (http://www.w3schools.com/ajax/ajax_server.asp):

          function getXMLHTTPReque stObject( )
          {
          var xmlHttp = null;
          try
          {
          // Firefox, Opera 8.0+, Safari
          xmlHttp=new XMLHttpRequest( );
          }
          catch (e)
          {
          // Internet Explorer
          try
          {
          xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" );
          }
          catch (e)
          {
          try
          {
          xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP");
          }
          catch (e)
          {
          alert("Your browser does not support AJAX!");
          }
          }
          }
          return xmlHttp;
          }

          Though, I'm still confused as to why the onreadystatecha nge method
          isn't being called in Firefox. Excuse me for being so dense. I'm
          trying to wrap my brain around this. So, basically, if I reach the
          portion of my JS that wants to parse the retrieved XML before
          xmlHttp.readySt ate==4, I'm hosed? I was thinking that using
          synchronous mode would avoid this.

          Comment

          • -Lost

            #6
            Re: XMLHttpRequest Object: Asynchronous vs Synchronous Mode

            "HugeBob" <rnurse@gmail.c omwrote in message
            news:1175193279 .258648.203270@ l77g2000hsb.goo glegroups.com.. .
            On Mar 29, 12:21 pm, "-Lost" <missed-s...@comcast.ne twrote:
            >"HugeBob" <rnu...@gmail.c omwrote in message
            >>
            >news:117518326 0.060842.183960 @d57g2000hsg.go oglegroups.com. ..
            >>
            >>
            >>
            On Mar 29, 10:58 am, "-Lost" <missed-s...@comcast.ne twrote:
            >"HugeBob" <rnu...@gmail.c omwrote in message
            >>
            >>news:11751776 91.794121.18750 0@l77g2000hsb.g ooglegroups.com ...
            >>
            Hi All,
            >>
            I've got a question about Asynchronous vs Synchronous mode with the
            XMLHttpRequest object. What are the ramifications of using one mode
            vs the other? If the script uses Asynchronous mode, it sounds as if a
            thread retrieves the data from the supplied URL and the JS function
            that called the open() and send() methods continues on. Where as
            using Synchronous mode the method that called open() and send() waits
            until the data from the URL has been retrieved. Am I correct in my
            thinking?
            >>>>
            >The ramifications of not using the asynchronous aspect of AJAX is having the UA or
            >browser
            >block activity until the request returns, finishes, loads, whatever.
            >>
            >I would not say that the open() and send() methods continue. The onreadystatecha nge
            >property still receives a dispatch however (even when blocking). So instead of
            >being
            >notified of what open() and send() are doing you will receive an onload event and
            >the
            >readyState of 4.
            >>
            >I would say you are incorrect in thinking that it cannot update its interface during
            >a
            >synchronous call (assuming you really meant onreadystatecha nge instead of open() and
            >send()).
            >>
            >I hope I worded all that correctly.
            >>
            >-Lost
            >>
            I find myself in the same state as you signature, LOL. Well, sort
            of. I've got an app that uses AJAX. The onreadystatecha nge method
            sets a global variable with XML it retrieves from the server when
            readyState is 4. The global variable is later searched via JS. The
            application works in IE when synchronous mode is used and not at all
            in FireFox 1.5 (the onreadystatecha nge method never seems to run).
            So, take the following function:
            >>
            globalVariable = null;
            function ajaxFunction( )
            {
            var xmlHttp;
            >>
            xmlHttp = getXMLHTTPReque stObject( );
            alert(xmlHttp);
            xmlHttp.onready statechange=fun ction()
            {
            if(xmlHttp.read yState==4)
            {
            globalVariable = xmlHttp.respons eText;
            }
            }
            >>
            xmlHttp.open("G ET","time.asp", true);
            xmlHttp.send(nu ll);
            }
            >>
            So, are you saying that even when this function completes,
            globalVariable may not have anything in it when it's accessed later
            regardless of whether I use asynch or synch mode?
            >>
            >No, globalVariable will have the responseText specified by the XMLHttpRequest.
            >However,
            >if asynchronous is set to false, depending on the size of the server's output (e.g. how
            >long it takes to load) globalVariable will still have the responseText, it may not be
            >available as quickly.
            >>
            >Assuming of course that getXMLHTTPReque stObject() returns the proper XMLHttpRequest.
            >>
            >To support the greatest number of possible browsers first have a look at:
            >>
            >http://jibbering.com/2002/4/httprequest.html
            >>
            >...the implementation there is fairly solid.
            >>
            >-Lost
            >
            Oh, my getXMLHTTPReque stObject() method handles that. I borrowed it
            from W3Schools (http://www.w3schools.com/ajax/ajax_server.asp):
            >
            <snip w3schools edited AJAX function>
            Though, I'm still confused as to why the onreadystatecha nge method
            isn't being called in Firefox. Excuse me for being so dense. I'm
            trying to wrap my brain around this. So, basically, if I reach the
            portion of my JS that wants to parse the retrieved XML before
            xmlHttp.readySt ate==4, I'm hosed? I was thinking that using
            synchronous mode would avoid this.
            Bearing in mind a flawless (near flawless) XHR implementation will not suffer this error.

            Could you perhaps offer a page of your implementation for further debugging?

            -Lost


            Comment

            Working...