How to serialize multiple jQuery AJAX requests?

by Admin 21. July 2009 11:32

Download Sample Project

In last post, Using jQuery to send AJAX requests, i discussed some basic concepts about how you can use simple .getJSON function to send AJAX requests using jQuery. For discussion of this post, I have added one more AJAX request into the mix. Now I want to another AJAX request to different target. But I want to make sure that second requests is only sent after first one is complete.

There is an async option that you can in jQuery. You can set this option to false to turn your AJAX request into a synchronous request. What this means is that your request is waiting till it completes. If you use this option you can write some code like below.


$(function(){
.getJSON("url1", "", callback1);
.getJSON("url2", "", callback2);
});

There are few issues with this approach. First, your first request will wait till timeout if there is any issue in network. So now your page is waiting as well to render. Second if your second request depends on completion or success of first one, then you do not have control. Second request will be sent no matter what. So setting async flag in general is not something you want to do. More elegent and very simple solution is to make second request in callback function of first request. This way your two calls are serialized now and you still enjoy benefits of async requests. In the sample project for clock synchronization, I have the following code snippet that demonstrates this method of ensuring that second request does not get fired before first one finishes.


function getServerTime() {
 cTime = firstRequest ? -1 : curTime.getTime();
 $.getJSON(clockServiceUrl, { clientTime: cTime, requestKey: respKey }, gotServerTime);
}

function getSiteUpdates() {
 cTime = firstRequest ? -1 : curTime.getTime();
 $.getJSON(siteUpdatesServiceUrl, { clientTime: cTime, requestKey: respKey }, gotSiteUpdates);
}

function gotSiteUpdates(data) {
if (data.MessageCount > 0) {
 elUpdatesPanel.append('

' + data.MessageCount + ' updates ' + '

'); } } function gotServerTime(data) { firstRequest = false; latency = data.Latency; curTime = new Date(parseInt(data.ServerTime)); if (!clockTicking) createClockTimer(); createServiceTimer(defaultServiceTimer); getSiteUpdates(); }

Views: 17081

Tags: , , , , , , ,

AJAX | AJAX | JQuery | JQuery

Comments

7/22/2009 2:54:38 AM #

trackback

Send AJAX requests serially using JQuery

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

11/25/2009 6:27:40 AM #

cash loans

Interesting post

cash loans United States

12/4/2009 4:02:48 AM #

online betting systems

Cool idea. I haven't thought of a need for it yet, but you could use this method for holding data on the page as well. You could put json in your comments, then do an eval(jComments.html()) to get the object.

online betting systems United States

12/5/2009 6:10:09 AM #

payday loans

Searching for this for some time now - i guess luck is more advanced than search engines :)

payday loans United States

12/21/2009 7:44:14 AM #

Michigan Payday Loans

I guess there's always an easier way ...

Michigan Payday Loans United States

12/22/2009 11:21:43 PM #

Web Hosting

Searching for this for some time now - i guess luck is more advanced than search engines :)

Web Hosting United States

12/26/2009 2:27:48 AM #

888 casino

Personally i think the 10 rules of web design that they came up with were good pointers for all to follow. i think if people followed step when developing a website there would be a lot more quality out there on the web.

888 casino United States

12/30/2009 9:57:43 AM #

faxless loans

Nice resource. rss feed added

faxless loans United States

1/18/2010 1:49:03 PM #

online payday loans

The people who get on in this world are the people who get up and look for the circumstances they want, and, if they can't find them, make them.

online payday loans United States

1/23/2010 1:52:11 AM #

cash advance

thanks again

cash advance United States

1/23/2010 12:19:40 PM #

payday loans

@michael - don't be so sure .

payday loans United States

2/4/2010 5:10:52 AM #

ucvhost

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work.

ucvhost United States

2/4/2010 5:54:20 AM #

lottery

I found lots of interesting information here. The post was professionally written and I feel like the author has extensive knowledge in the subject. Keep it that way.

lottery United States

2/5/2010 9:06:31 AM #

Chapel Hill Homes

The blog was absolutely fantastic!
Lots of great information and inspiration, both of which we all need!

Chapel Hill Homes United States

2/6/2010 7:21:39 AM #

ucvhost

How to use Remote Desktop Connection for the two systems which have same IP Address?

ucvhost United States

2/12/2010 3:07:28 AM #

ucvhost

i. I wanted to drop you a quick note to express my thanks. I've been following your blog for a month or so and have picked up a ton of good information as well as enjoyed the way you've structured your site.

ucvhost United States

2/21/2010 11:07:56 PM #

bookmarksite

I found lots of interesting information here. The post was professionally written and I feel like the author has extensive knowledge in the subject. Keep it that way.

bookmarksite United States

2/21/2010 11:17:47 PM #

easybookmarking

Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share.

easybookmarking United States

2/22/2010 3:22:14 AM #

Reviews Of Registry Cleaners

Thanks for this post - I really like what you've done here and hope you get the most benefits from it.

Reviews Of Registry Cleaners United Kingdom

2/22/2010 3:23:10 AM #

Reviews Of Registry Cleaners

A good post. I love using AJAX and JQuery is just a dream to work with as well.

Reviews Of Registry Cleaners United States

2/22/2010 3:24:30 AM #

Reviews Of Registry Cleaners

Thanks for this post. I really like using this tool and hope you get the most out of it.

Reviews Of Registry Cleaners United States

2/22/2010 3:25:04 AM #

Reviews Of Registry Cleaners

Thank you.

Reviews Of Registry Cleaners United States

2/23/2010 4:51:02 PM #

compare dental plans

So you probably need the wamp or xamp to make it the local host?just make this explanation   simpler since its important

compare dental plans United States

3/8/2010 2:49:44 AM #

online personal loan application

thanking you for providing such a great help

online personal loan application United States

3/18/2010 11:17:11 PM #

forum link building service

Very useful info. Hope to see more posts soon! They are one of leading link building services provider. these gusys are into web market for the past 5 years serving many clients. they are  specialized in getting long term higher rankings by boosting your SEPRs. i have gone through that i got lots of  experience from them..

forum link building service United States

Powered by BlogEngine.NET 1.5.1.7
Theme by Naveen Kohli