Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  20] [ 7]  / answers: 1 / hits: 18459  / 12 Years ago, fri, september 14, 2012, 12:00:00

I am completely new to Javascript/jquery world and need some help. Right now, I am writing one html page where I have to make 5 different Ajax calls to get the data to plot graphs. Right now, I am calling these 5 ajax calls like this:



$(document).ready(function() {
area0Obj = $.parseJSON($.ajax({
url : url0,
async : false,
dataType : 'json'
}).responseText);

area1Obj = $.parseJSON($.ajax({
url : url1,
async : false,
dataType : 'json'
}).responseText);
.
.
.
area4Obj = $.parseJSON($.ajax({
url : url4,
async : false,
dataType : 'json'
}).responseText);

// some code for generating graphs

)} // closing the document ready function


My problem is that in above scenario, all the ajax calls are going serially. That is, after 1 call is complete 2 starts, when 2 completes 3 starts and so on .. Each Ajax call is taking roughly around 5 - 6 sec to get the data, which makes the over all page to be loaded in around 30 sec.



I tried making the async type as true but in that case I dont get the data immediately to plot the graph which defeats my purpose.



My question is:
How can I make these calls parallel, so that I start getting all this data parallely and my page could be loaded in less time?



Thanks in advance.


More From » jquery

 Answers
127

Using jQuery.when (deferreds):



$.when( $.ajax(/req1), $.ajax(/req2), $.ajax(/req3) ).then(function(resp1, resp2, resp3){ 
// plot graph using data from resp1, resp2 & resp3
});


callback function only called when all 3 ajax calls are completed.


[#83080] Wednesday, September 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emanir

Total Points: 151
Total Questions: 90
Total Answers: 105

Location: Suriname
Member since Sun, Jun 13, 2021
3 Years ago
;