lundi 9 février 2015

Cant do multiple queries in a For-Loop in Javascript [duplicate]


This question already has an answer here:




I need to send a query to my database and then get the results from database to my web app. For that purpose i wrote a script and it works properly for one query at a time. But when i want to do multiple queries in a for loop , it does only for one random query, not for all. Here is my code :



function updateBill() {
for (id = 105; id < 110; id++) {
var sum_param = '#sum_' + id;
var val1_param = '#startdate_' + id;
var val2_param = '#lastdate_' + id;
var startreading_param = '#startreading_' + id;
var lastreading_param = '#lastreading_' + id;
var periodreading_param = '#periodreading_' + id;
$.ajax({
url: "submit",
type: "POST",
data: {main_id: id, unitprice: $("#unitprice").val(), val1: $(val1_param).val(), val2: $(val2_param).val()},
success: function(response) {
$(sum_param).html(response['4']);
$(startreading_param).html(response['1']);
$(lastreading_param).html(response['2']);
$(periodreading_param).html(response['3']);
}
});
};
};


Maybe without its finishing current query and json transfer and tries to do another query and thats why it does so. If so, how can i fix the code to go to next iteration when the current one is totally finished its task? Could you give me some insights about that , please?


Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire