Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast way to make multiple AJAX calls

So i have a web application which is making around 14-15 AJAX calls to some APIs. The problem is the amount of time all the AJAX calls takes is nearly 3x than the time in which each individual API shows me response when I type its URL in the browser.

I am making all the AJAX calls instantly inside the DOM Ready event.

The thing is how can i speed up this process of making 15 AJAX calls together, getting the response as fast as possible and Manipulating the DOM accordingly.

Few Points which i have in my mind:

  1. All the AJAX calls should be ASYNC in nature. (Already doing it).
  2. Don't make all the AJAX calls at the same time. Induce some sort of timeout as making all the AJAX calls at the same time may block the bandwidth and slows down the turn around time of the process.
  3. Reducing the number of API calls by any means. (Already doing it).
  4. Manipulate the DOM as minimal as possible. (Already doing it).
  5. Setting cache:true in AJAX setup. I don't think that will really help, still i am doing it wherever i am sure content will update really slow.

Any suggestions will be valuable! Thanks.

The way i am making AJAX calls

$(document).ready(function(){

    loadSentimentModule();
    loadCountModule();
    loadEntitiesModule();  
    // Some more function calls.

});

function loadSentimentModule(){

   $.ajax({
          url:someurl,
          cache:true,
          dataType:"json",
          success: function(data){

              // Based on data manipulating DOM.
          }

}

// Same kind of function defintions for all the functions.
like image 664
void Avatar asked Nov 27 '15 09:11

void


People also ask

How do I handle multiple AJAX requests?

The jQuery library provides a way to make any callback function waiting for multiple Ajax calls. This method is based on an object called Deferred. A Deferred object can register callback functions based on whether the Deferrred object is resolved or rejected. Here is an example of the Deferred object.

How do you send AJAX request every 5 seconds?

Use just setTimeout(executeQuery, 5000); instead of setTimeout('executeQuery()', 5000); - it's shorter and faster.


2 Answers

You may not issue the ajax call directly, but queue them and let a manager control the queue, see here: Queue ajax requests using jQuery.queue()

like image 76
Axel Amthor Avatar answered Oct 19 '22 08:10

Axel Amthor


I recomend you to use the async.js module on client. May be it this what are you looking for.

like image 1
Roman A. Avatar answered Oct 19 '22 07:10

Roman A.