在進入下一次迭代之前是否可以讓此循環等待直到AJAX調用完成?讓循環等待ajax調用
for(curr;curr < total;curr++) {
$this.val('Uploading '+(curr+1)+' out of '+total);
var tmpStr = JSON.stringify(o[curr]).replace("&", "and");
tmp[0] = JSON.parse(tmpStr);
$.ajax({
type: 'POST',
url: 'http://example.com/api/post',
data: "data="+JSON.stringify(tmp),
success: function(msg) {
var d = JSON.parse(msg);
if (d.success) {
//localStorage.removeItem("data");
}
else
{
alert("Data failed to upload, please try again");
}
},
error: function(msg) {
alert("Data failed to upload, please try again");
$this.val('Upload to CMS');
}
});
}
迂迴地,沒有。它不是** a ** jax,如果它是同步的,但我建議你重構你的代碼,而不是使其同步 –