I'm making a script for some large queries and to avoid timeouts on php side, plus giving the user some input, I'm trying to make most of it on jquery.
So it works like this: First the user select one record on a select. When he clicks the button, I got a ajax call to get the details I need from that record on json format.
With the response, I got a loop that makes ajax calls for each result from that first call.
If I set async to false it works, but the browser freezes because there are several ajax calls. So is there a way to update the ui with async turned off?
I mean, the fact I need to set async to off when I'm making ajax calls inside a loop doesn't make any sense for me. It can make severals calls at same time, I'm fine with that, but why doesn't it get the current index inside that loop unless I set async to false? It keeps using the first index.
$("#button_copy").live('click', function(){
$('#button_copy').attr("disabled", true);
id = $('#select').val();
$.ajax({
type: "POST",
url: "query1.php",
dataType: 'json',
async:false,
cache: false,
data: 'id=' + id,
success: function( data ) {
var total1 = data['records'].length;
$("#console").append('Total query 1: ' + total1 + '<br />');
for (var i in data['records']) {
$.ajax({
type: "POST",
url: "query2.php",
dataType: 'json',
async:false,
cache: false,
data: 'id=' + data['records'][i].id,
success: function( data2 ) {
var total2 = data2['records2'].length;
$("#console").append('Total of query 2 from ' + data['records'][i].person + ': ' + total2 + '<br />');
},
error: function(data2) {
console.log(data2);
}
});
var percentprogress1 = (i / total1) * 10000;
$('#progressbar1').css('width',percentprogress1 + '%');
}
},
error: function(data) {
console.log(data);
}
});
});