Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery $.get(...) one at a time

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.

like image 737
Basit Avatar asked Dec 06 '25 00:12

Basit


1 Answers

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
  }
});
like image 200
Pablo Fernandez Avatar answered Dec 07 '25 12:12

Pablo Fernandez