multiple xmlhttprequests getting lost

Discussion in 'Javascript' started by jeff.maildump, Mar 7, 2007.

  1. Hi,

    I've got multiple xmlhttprequests which are in a loop. So this is the
    loop I have so far, with the closure given to me in a previous post:

    for (i=0; i<data.length; i++) {
    httpRequest = false;
    httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = function(index) {
    return function () {
    if (httpRequest[index].readyState == 4) {
    if (httpRequest[index].status == 200) {
    var stats = getStats(httpRequest[index].responseText,
    updateStats(statsTable.rows[index], stats);
    }(i);'GET', URL, true);


    So basically, the getStats method parses the response to get the info
    I need for that particular request and then updates the html of the
    current page accordingly. This works fine for 1, 2, or 3 data
    requests. But if I have more, some of the httprequests get lost and
    not all parts of my page are updated. But, if I throw in an alert in
    each loop, all page data or at least more of them are updated, once
    per time after each loop. It's like the alerts slow the loop down and
    allow the requests and updates to catch up to the loop processing.

    So, I'm not sure what's happening when the alerts aren't there. Are
    some of the requests getting clobbered somehow? Are the httprequests
    fine, and maybe the original document can only be updated once at a
    jeff.maildump, Mar 7, 2007
    1. Advertisements

  2. Actually, the alerts totally stop the loop in its place until you
    click ok on the alert box. So you're right, with the alerts in there
    you're allowing the request and updates to catch up.

    I recommend finding a better way to debug than using alerts, since
    they change how your code works. I use an amazing firefox extension,
    FireBug, to debug javascript. Instead of alert('request sent'); you
    do console.log('request sent');, and it will just print it out to the
    firebug console which will be at the bottom of your firefox.

    Not exactly sure how to solve your bigger problem, but this should
    solve your debugging problem.

    good luck
    Dan Silberman, Mar 7, 2007
    1. Advertisements

  3. Yeah, I like that log idea. I've just recently started using venkman
    with these scripts, but the way I have it now, it halts execution at
    whatever break point I setup and I figured it'd be the same as an
    alert. I'll look into firebug. Thanks.
    jeff.maildump, Mar 8, 2007

  4. Ok so firebug seems to work well, but instead of just console.log() I
    have to put
    jeff.maildump, Mar 8, 2007
  5. jeff.maildump

    whiskey Guest

    I've got multiple xmlhttprequests which are in a loop.

    Well, that could be the problem: "multiple xmlhttprequests in a loop".
    The standard says a client *should not* maintain more than 2
    connections with any server (see RFC 2616). By default, Firefox (and
    IE, I believe) makes 2 connections, while Opera makes 8. Of course,
    this may not be the real problem, it's only an hypothesis. However,
    rewrite your code in order to keep track of which requests are sent
    and which requests have a response, so you only make a maximum of 2
    connection at a time. Example:
    Suppose you want to make 5 requests: A, B, C, D, and E. The order
    could be:
    A -> request
    B -> request
    A <- response
    C -> request
    B <- response
    D -> request
    D <- response
    E -> request
    C <- response
    E <- response
    The order may not be like this one, depending on the time needed for
    the server to send a response.

    If, however, this is not the issue you're having, you shoudln't try to
    make more than 2 requests at a time in any app.
    whiskey, Mar 12, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.