0

I have a jQuery Ajax function that requests data from the server.

The code finds all spans, gets their IDs and sends it to the json.php for a result. Each id returns a different value and their load times are different. Say one loads instantly and the other loads in 10 seconds.

Whats wrong: Ok, once the page loads, all requests are sent to json.php, but if the third span is going to return a result instantly, it WAITS for the first two to load first!!

Scenario: I have 3 spans, the first one must be returned in 9 seconds, the seconds one must be returned in 4 seconds, and the third one must load in 5 seconds. Here is what happens: The first span appears after 9 seconds, the seconds span appears after 9+4 seconds and the last one loads in 9+4+3 seconds.

Here is the code:

<script>
function getData(d){
    var r=Math.floor(Math.random()*500)
    $('#'+d).addClass('load');
    var handler = '';
    $.get('json.php?request='+d+'&r='+r, function(data) {
                var obj = jQuery.parseJSON(data);
                for (var i = 0; i < obj.length; i++) {
                    var object = obj[i];
                    for (property in object) {
                            handler = object[property];
                                $('#'+d).removeClass('load');
                                $('#'+d).html(handler);
                    }
                }
    });
    return true;
}

$(document).ready(function() {
    $("span").each(function() {               
         getData($(this).attr('id'));
     });
});
</script>

            <span id="first_data"></span>
            <span id="second_data"></span>
            <span id="third_data"></span>

Anything wrong here??

Reza
  • 684
  • 1
  • 10
  • 22
  • I don't understand your question. Are you asking why the response times are different on the three ajax calls? Are you asking how to sequence the ajax results so the results display in a particular order? Are you asking how to show the results with a particular timing and order between span? – jfriend00 Oct 20 '11 at 02:12
  • I want each span appears accordingly. e.g. span 2 only takes 3 seconds to load, so I want the result to appears in 3 seconds. span 1 takes 20 seconds to load, so I want that appears in 20 seconds... Here span 2 takes 23 seconds, not 3 seconds. And the request is being done Asynchronously, so I dont really get it. – Reza Oct 20 '11 at 02:20
  • I'd suggest looking at the Net panel in Firebug (free Firefox add-on) while your page starts up and see if the ajax requests are being held by the browser or not. – jfriend00 Oct 20 '11 at 02:41
  • Actually they are! When I check Firebug, they are being executed asynchronously, but they stay in loading state once the request before is done. – Reza Oct 20 '11 at 02:44
  • It could also be that your server can only handle one request at a time so they get serialized there. – jfriend00 Oct 20 '11 at 04:58
  • Good point. Do you have any suggestion on how to check that and to be sure? Im on CentOs Apache thanks – Reza Oct 20 '11 at 05:37
  • Whether your server app can handle more than one request at a time depends upon the type of server app, its implementation and your configuration. To test, you could line up three separate browsers on the same computer and make requests from each one at the same time and see if all nine requests seem serialized. That would rule out a single browser serializing them all. – jfriend00 Oct 20 '11 at 06:10

1 Answers1

0

What browser?

How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?

http://www.stevesouders.com/blog/2008/03/20/roundup-on-parallel-connections/

Community
  • 1
  • 1
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Btw, great point. However I call my function in document.ready, that makes sure all other requests are finished loading. But I'm sure I have an issue with concurrency. – Reza Oct 20 '11 at 03:32
  • @Reza It's not about having the other requests finished loading, it's about only being able to have *n* Ajax requests pending, but in FF7/8 you get more than two, so it's something else. – Dave Newton Oct 20 '11 at 03:34