Is $.post asynchronous? So if I did the commands like so below would they all be done at once instead of being synchronous (1 done, wait, then the next, etc.) ?
$(document).ready(function() { var variable1 = '1'; var variable2 = '2'; var variable3 = '3'; var variable4 = '4'; $.post("process.php", {element1: variable1}, function(data){ $("#area1").html(data); }); $.post("process.php", {element2: variable2}, function(data){ $("#area3").html(data); }); $.post("process.php", {element3: variable3}, function(data){ $("#area4").html(data); }); $.post("process.php", {element4: variable4}, function(data){ $("#area4").html(data); }); }); 33 Answers
By default $.post() function is asynchronous
If you want it to be synchronous Samuel J Mathew's is correct. But if you want to keep using $.post() instead of $.ajax(), you should do it this way:
$.ajaxSetup({async: false}); $.post(); remember to set async back to true. Good Luck!
All ajax request are asynchronos as it acronym suggest. (Asynchronous Javascript and XML)
You can refer the api documentation of $.post() in Jquery website
Which clearly says that
This is a shorthand Ajax function, which is equivalent to:
$.ajax({ type: "POST", url: url, data: data, success: success, dataType: dataType });The success callback function is passed the returned data, which will be an XML root element or a text string depending on the MIME type of the response. It is also passed the text status of the response.
From above extract we can understand that $.post() is asynchronous.
For jquery versions < 1.8 you can use async:false to make it synchronous. Afterwards it is depreciated. Anyway it is not a recommended practice to use this parameter
For jquery versions >= 1.8 you can use .done() callback. in which you can add all your operations which you need to perform after ajax call
The most elegant solution to what you want to do would be to store all the data in a single JSON and send it over to your php and then answer with another JSON and decode the data so you can update all in a single call. I'd code it but i don't know how your php or html works.