0

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.

asked Aug 9, 2018 at 22:07
11
  • "I have set async to true so had assumed these calls would all happening at the same time" You do understand what asynchronous means don't you? Commented Aug 9, 2018 at 22:10
  • 1
    Don't have to set async:true. That is default. If there is much of a delay it is likely server side related. Inspect the actual requests in browser dev tools network Commented Aug 9, 2018 at 22:10
  • 1
    Also some of those options are useless on a GET request Commented Aug 9, 2018 at 22:13
  • 1
    You aren't sending any data so processing is pointless as is mimeType and contentType on a GET there is no content sent and crossDomain is really only for testing jsonp Commented Aug 9, 2018 at 22:16
  • 3
    Chnage your server script so that it sleeps for a random number of seconds. Then you'll see that the log messages happen out of order. Commented Aug 9, 2018 at 22:48

1 Answer 1

1

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.

answered Aug 9, 2018 at 22:16
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.