I have problem to make delays between ajax requests in loop. I want script to wait 7 seconds to do next one. Requests are not identical, and I don't know how many of them can be.
$(document).ready(function () {
var announce = $("#announce").data('id');
var lots_cnt = parseInt($("#announce").data('lotscnt'));
for (var i = 0; i < Math.ceil(lots_cnt/20); i++) {
$.ajax({
method: "GET",
url: "/project/lots/"+announce+"/"+(i+1),
async: false,
beforeSend: function () {
$("#console").append("<strong>Parsing lots from page "+(i+1)+"...</strong><br/>");
},
complete:function(){
},
success: function (m) {
$("#console").append(m);
addprogressstep();
setTimeout(function() { $("#console").append("Waiting 7 sec ...<br/>"); }, 7000);
},
error:function(jqXHR, textStatus, errorThrown){
}
});
};
});
setTimeout(). That said, sending that many AJAX requests isn't a great idea if it can be avoided. I'd suggest looking in to sending all data in a single request, if possible. However, you should definitely removeasync: falseas it's horrendous practice - if you check the console you'll even see the browser telling you not to use it.