Executing multiple XMLHttp requests

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

    Executing multiple XMLHttp requests

    I'm trying to figure out how to iterate over an array that would send
    a series of XMLHttp requests. I'd like to have each request finish
    before the next one begins, but I believe that this is not possible to
    do in a straightforward fashion, given the normally asynchronous
    method of XMLHttp. The (untested, pseudo) code below gives a little
    idea of what the goal is, but I'm not really sure how to program it
    elegantly.

    One thought I had is to compare two arrays, one with the desired IDs
    to be completed, and another with the IDs that have been completed.
    The callback handler would add IDs as each request is completed, and
    then restart the process until the two arrays match exactly.

    Is there any more elegant way?

    Thanks for any pointers.

    --Brent

    ------------------------------------------------------------------------

    function updateLists()
    {
    var listIds = "3,7,42,157 ";

    var listArr = listIds.split(' ,');

    for(i = 0; i < listArr.length; i++)
    {
    doUpdate(listAr r[i]); //<--How do I make the loop wait for
    doUpdate() to complete before proceeding?
    }
    }


    function doUpdate(listId )
    {
    //executes XMLHttp request on server and sets up callback
    handler, showListStatus
    }

    function showListStatus( callbackRespons e)
    {
    //Code that updates page with result from server.
    //This could add to an array of finished IDs, and then call
    updateLists() again, where
    //the array of finished IDs would be compared to the array of
    desired IDs
    }
  • Bjoern Hoehrmann

    #2
    Re: Executing multiple XMLHttp requests

    * Brent wrote in comp.lang.javas cript:
    >I'm trying to figure out how to iterate over an array that would send
    >a series of XMLHttp requests. I'd like to have each request finish
    >before the next one begins, but I believe that this is not possible to
    >do in a straightforward fashion, given the normally asynchronous
    >method of XMLHttp. The (untested, pseudo) code below gives a little
    >idea of what the goal is, but I'm not really sure how to program it
    >elegantly.
    If you want to download the resources in parallel this is relatively
    simple, just download the "next" one from the ready state change handler
    when it's indicating that the download completed. Keep track of the
    resources to download e.g. by an array that you pop from each time,
    until it's empty. If you want to do something after all the downloads
    are complete, do that too from there. This is pretty much the only way
    since you cannot otherwise wait for an operation to complete.
    --
    Björn Höhrmann · mailto:bjoern@h oehrmann.de · http://bjoern.hoehrmann.de
    Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
    68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/

    Comment

    • =?ISO-8859-1?Q?Une_B=E9v?==?ISO-8859-1?Q?ue?=

      #3
      Re: Executing multiple XMLHttp requests

      Bjoern Hoehrmann <bjoern@hoehrma nn.dewrote:
      This is pretty much the only way
      since you cannot otherwise wait for an operation to complete.
      do you mean even with a "window.setTime out" ? something like :

      function everythingDownl oaded(){
      if(isEverything Downloaded){
      do next job
      } else {
      window.setTimeo ut(everythingDo wnloaded(),100)
      }
      }
      ???
      --
      Une Bévue

      Comment

      • Tom de Neef

        #4
        Re: Executing multiple XMLHttp requests

        "Brent" <writebrent@gma il.comschreef in bericht
        news:c4637a8c-7fb7-41a1-a84a-8384582a380e@k1 g2000prb.google groups.com...
        I'm trying to figure out how to iterate over an array that would send
        a series of XMLHttp requests. I'd like to have each request finish
        before the next one begins, but I believe that this is not possible to
        do in a straightforward fashion, given the normally asynchronous
        method of XMLHttp. The (untested, pseudo) code below gives a little
        idea of what the goal is, but I'm not really sure how to program it
        elegantly.
        >
        One thought I had is to compare two arrays, one with the desired IDs
        to be completed, and another with the IDs that have been completed.
        The callback handler would add IDs as each request is completed, and
        then restart the process until the two arrays match exactly.
        >
        Is there any more elegant way?
        >
        You can call XMLHttpRequest. open("GET",URL, false); the third argument says
        'not asynchronous' and that seems to be just what you are trying to do.
        Tom


        Comment

        • Bjoern Hoehrmann

          #5
          Re: Executing multiple XMLHttp requests

          * Une Bévue wrote in comp.lang.javas cript:
          >Bjoern Hoehrmann <bjoern@hoehrma nn.dewrote:
          >
          >This is pretty much the only way
          >since you cannot otherwise wait for an operation to complete.
          >
          >do you mean even with a "window.setTime out" ? something like :
          >
          >function everythingDownl oaded(){
          if(isEverything Downloaded){
          do next job
          } else {
          window.setTimeo ut(everythingDo wnloaded(),100)
          }
          >}
          That's not waiting, that's polling, and yes that would be possible but
          rather ugly, you could just do the same you would do here in the ready
          state change handler.
          --
          Björn Höhrmann · mailto:bjoern@h oehrmann.de · http://bjoern.hoehrmann.de
          Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
          68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/

          Comment

          • Brent

            #6
            Re: Executing multiple XMLHttp requests

            That's not waiting, that's polling, and yes that would be possible but
            rather ugly, you could just do the same you would do here in the ready
            state change handler.
            --
            Björn Höhrmann · mailto:bjo...@h oehrmann.de ·http://bjoern.hoehrmann.de
            Weinh. Str. 22 · Telefon: +49(0)621/4309674 ·http://www.bjoernsworld.de
            68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 ·http://www.websitedev.de/
            Björn:

            You wouldn't happen to have some example code, would you? I've looked
            all over for ways to load up the ready state handler with responses
            from multiple requests, but I'm coming up short.

            Thanks in advance.

            --Brent

            Comment

            • Bjoern Hoehrmann

              #7
              Re: Executing multiple XMLHttp requests

              * Brent wrote in comp.lang.javas cript:
              >You wouldn't happen to have some example code, would you? I've looked
              >all over for ways to load up the ready state handler with responses
              >from multiple requests, but I'm coming up short.
              It would look like this:

              var urls = [ 'c', 'b', 'a' ];
              var xhro = new XMLHttpRequest( );

              xhro.onreadysta techange = function() {

              // Do nothing while the object is downloading
              if (xhro.readyStat e != XMLHttpRequest. UNSENT &&
              xhro.readyState != XMLHttpRequest. DONE)
              return;

              if (urls.length == 0) {
              // everything has been downloaded
              return;
              }

              // Download the next document
              xhro.open("GET" , urls.pop(), false);
              xhro.send();
              }

              // call it manually for the first url.
              xhro.onreadysta techange();

              Note that this is unlikely to work in current implementations . The key
              idea is simply to check whether the previous request has finished, if
              not you continue, otherwise you start a new one, until there is nothing
              left to do.
              --
              Björn Höhrmann · mailto:bjoern@h oehrmann.de · http://bjoern.hoehrmann.de
              Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
              68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/

              Comment

              • Todd Wade

                #8
                Re: Executing multiple XMLHttp requests

                On Apr 26, 12:38 pm, Brent <writebr...@gma il.comwrote:
                I'm trying to figure out how to iterate over an array that would send
                a series of XMLHttp requests. I'd like to have each request finish
                before the next one begins, but I believe that this is not possible to
                do in a straightforward fashion, given the normally asynchronous
                method of XMLHttp.
                I didnt read through your code because I had to stop right here.

                It is quite simple to execute synchronous http requests with
                xmlhttprequest. From From http://ajaxpatterns.org/XMLHttpRequest_Call#Solution:

                var xhReq = new XMLHttpRequest( );
                xhReq.open("GET ", "sumGet.phtml?f igure1=5&figure 2=10", false);
                xhReq.send(null );
                var serverResponse = xhReq.responseT ext;
                alert(serverRes ponse); // Shows "15"

                The asynchronous version:

                var xhReq = createXMLHttpRe quest();
                xhReq.open("GET ", "sumGet.phtml?f igure1=5&figure 2=10", true);
                xhReq.onreadyst atechange = onSumResponse;
                xhReq.send(null );
                ...
                function onSumResponse() {
                if (xhReq.readySta te != 4) { return; }
                var serverResponse = xhReq.responseT ext;
                ...
                }

                Todd W.

                Comment

                Working...