fightstarr20
fightstarr20

Reputation: 12568

jQuery - Ajax async not working?

I have a simple Ajax call in jQuery like this...

    function my_call(identifier) {

    var settings = {
        "async": true,
        "crossDomain": true,
        "url": "http://www.example.com/?" + identifier,
        "method": "GET",
        "headers": {
        "Accept": "application/json",
    },

    "processData": false,
    "contentType": false,
    "dataType"   : "json",
    "mimeType": "multipart/form-data",
    }

    $.ajax(settings).done(function (response) {         
        console.log(response);
    });

}

And I am calling it 10 times for different identifiers like this...

my_call(1);
my_call(2);
my_call(3);
my_call(4);
my_call(5);

I have set async to true so had assumed these calls would all happening at the same time, but instead I seem to get the results print to the console one after the other with a bit of a delay in between.

Am I doing it correctly, or is there a different way to call the function? I am trying to reduce the time it takes to return the results.

Upvotes: 0

Views: 537

Answers (1)

Unmitigated
Unmitigated

Reputation: 89139

The AJAX function calls are occcurring one after another. They are still asynchronous as they are not waiting for the previous AJAX calls to finish before executing (which would be the behavior had you set async to false). AJAX calls are supposed to work that way; you send a request over to the server and wait for the response to execute a function. To make the AJAX calls finish faster, you will have to optimize your server-side code (if possible). Also, it is unnecessary to set the async property to true as that is the default. Javascript is single-threaded, so you cannot execute many functions at the exact same time.

Upvotes: 1

Related Questions