views:

64

answers:

3

i have a page with many actions on it, which triggers with $.get, but i want to run one at a time, rather then all triggering at once, that is lots of load time.. so what is the solution for me on this?

well before you give answer.. i dont want to trigger based on time, i want to trigger after each request is completely done from ajax, then go and continue with loop for ajax after first one is done.

+1  A: 

It sounds like you want an ajax queue.

I've used this plugin before, and it's pretty simple.

harpo
+3  A: 

Do you want to execute synchronous requests? if so, you need to use jQuery's ajax method instead of get, setting async:false.

check http://api.jquery.com/jQuery.ajax/

EDIT

As one commenter properly pointed, making sync requests hangs the only thread javascript code has. That means no animations or other code running while you wait for the requests to finish.

An interesting option would be to "chain" your requests, executing the next request in the previous one callback like this:

$.get('ajax/first-call.html', function(data) {

  $.get('ajax/second-call.html', function(data){
    //etc
  }
});
Pablo Fernandez
be careful with synchronous requests as the browser may hang whilst the request is in process.
Russ Cam
what you think of queue as andew prefered
Basit
+1  A: 

You can setup your own custom queue in jQuery.

http://api.jquery.com/queue/

  1. Populate your queue with all the functions you want to execute.
  2. Each function is a single call to $.get().
  3. In the callback for each $.get function, call the dequeue() function to start up the next ajax call.
AndrewDotHay