views:

923

answers:

7

I use jQuery. And I don't want concurrent AJAX calls on my application, each call must wait the previous before starting. How to implement it? There is any helper?

UPDATE If there is any synchronous version of the XMLHttpRequest or jQuery.post I would like to know. But sequential != synchronous, and I would like an asynchronous and sequential solution.

+5  A: 

You have two choices that I can think of. One is to chain them through callbacks. The other is to make the calls synchronous rather than async.

Is there a reason you want them sequential? That will slow things down.

To make the call synchronous, you'll set the async option in the Ajax call to false. See the documentation at http://docs.jquery.com/Ajax/jQuery.ajax#options (click options tab to see them).

Nosredna
How to make them synchronous?
Jader Dias
Or a third option: a class that would manage a queue of requests, and process them sequentially.
Jader Dias
I edited my answer to point to the Ajax options. Set the async option to false.
Nosredna
Yes. You could have your Ajax callback go to a handler that starts the next request. I guess that might be easier to deal with than manual chaining through the callbacks.
Nosredna
+1  A: 

Look at this: http://docs.jquery.com/Ajax/jQuery.ajax (click on the "options" tab).

But remember a synchronous call will freeze the page until the response is received, so it can't be used in a production site, because users will get mad if for any reason they have to wait 30 seconds with their browser frozen.

EDIT: ok, with your update it's clearer what you want to achieve ;)

So, your code may look like this:

 $.getJSON("http://example.com/jsoncall", function(data) {
  process(data);
  $.getJSON("http://example.com/jsoncall2", function (data) {
   processAgain(data);
   $.getJSON("http://example.com/anotherjsoncall", function(data) {
    processAgainAndAgain(data);
   });
  });
 });

This way, the second call will only be issued when the response to the first call has been received and processed, and the third call will only be issued when the response to the second call has been received and processed. This code is for getJSON but it can be adapted to $.ajax.

FWH
I am aware of your solution, but the multiple request aren't generated by the same even, so I can't chain them together as you showed. My solution would be more like a lock(globalAjaxObject) in C#
Jader Dias
+1  A: 

The best way you could do this is by chaining callbacks as Nosredna said. I wouldn't recommend using synchronous XMLHttpRequest as they lock your entire application.

There aren't much helper for this as far as I know, but you could do something resembling a callback FIFO.

Gab Royer
+2  A: 

You could give narrative javascript a try http://www.neilmix.com/narrativejs/doc/

I've never used it myself though. If I wanted to do this, I would setup some kind of abstraction for chaining asynchronous actions. As others have said, the synchonous version of the ajax object blocks events from being processed while it's waiting for a response. This causes the browser to look like it's frozen until it recieves a response.

Breton
That looks cool.
Nosredna
Thanks for the addition, but I am not sure if it addresses my problem. It makes async calls sequential if there was only one generating thread, but in my case many events spawns requisitions.
Jader Dias
Like I said, I haven't used NJS, but its description says that it adds a yield operator that simply stops execution of a function until an event has fired. It doesn't seem to me like that would prevent concurrency. If it did, it wouldn't be a very useful library to anyone.
Breton
s/library/compiler/
Breton
If nothing else, you could study the compiled code and you may find a plain JS solution to your problem that way.
Breton
It would be useful to me =)
Jader Dias
A: 

Set the async option to false, e.g.,

$.ajax({ async: false /*, your_other_ajax_options_here */ });

Reference: Ajax/jQuery.ajax

Joe Chung
+6  A: 

Check this plugin:

This plugin basically helps you to manage "Ajax race conditions".

When multiple Ajax requests are made in rapid succession, the results can be returned out of order.

CMS
A: 

Synchronous calls aren't necessarily slower, if you have an app where AJAX calls open, posts to, then closes a socket, multiple calls to the socket don't make sense as some sockets can only handle a single connection, in which case, queuing data so its only sent when the previous AJAX call has completed means much higher data throughput.

tomski777