function pdfToImgExec(file, IsfirstLogging, folder, round) {
alert(file);
var postString = file + '&' + IsfirstLogging + '&' + folder + '&' + round;
var errorMsg = (folder == 'Incoming' ? '<p>error in incoming folder</p>' : '<p>error in other folder</p>');
$.ajax({
type: "POST",
cache: false,
async: false,
url: "pdfToImgExec.php",
data: {
"data": postString
},
dataType: "html",
beforeSend: function () {
alert(file + 'a');
$('#pdfToImgResult').html('<p>Converting' + file + ', Please wait......</p>');
},
success: function (data) {
if(data == '1') {
$('#pdfToImgResult').html('<p>Complete convert ' + file + '</p>');
} else if(round < 4) {
$('#pdfToImgResult').html('<p>Fail to convert , retry ' + round + ' round <img src="loading.gif" height="20" width="20"/></p>');
round++;
pdfToImgExec(file, 'false', folder, round);
} else {
folder == 'Incoming' ? tempFailIncomingFiles.push(file) : tempFailResultFiles.push(file);
}
},
error: function (x, t, m) {
$('#pdfToImgResult').html(errorMsg);
alert(t);
releaseBtn();
}
});
}
这个ajax调用的问题是我可以在beforeSend函数中提醒(文件+'a'),但是
$('#pdfToImgResult').html('<p>Converting' + file + ', Please wait......</p>');
不起作用,它不会显示任何内容,只会跳转到
$('#pdfToImgResult').html('<p>Complete convert ' + file + '</p>');
在ajax调用结束后。
归因于async:false
吗?如何解决问题?感谢。
答案 0 :(得分:1)
这是因为您正在使用async: false,
,因此该函数会阻止请求完成,从而阻止重绘,直到完成所有操作。
您似乎都设置了回调,因此似乎没有任何理由来制作阻止xhr请求。只需删除async: false,
,即可完成设置。
这是一个如何处理异步代码的简单示例。我删除了大部分代码以保持简洁。
// --------------------------------new parameter-------------v
function pdfToImgExec(file, IsfirstLogging, folder, round, callback) {
// your code...
$.ajax({
type: "POST",
cache: false,
// async: false, // Remove this line!
url: "pdfToImgExec.php",
data: {
"data": postString
},
dataType: "html",
beforeSend: function () {
$('#pdfToImgResult').html('<p>Converting' + file + ', Please wait......</p>');
},
success: function (data) {
// your code...
// Invoke the callback, passing it the data if needed
callback(data)
},
error: function (x, t, m) {
// your code;
}
});
}
当您调用pdftoImgExec
时,将函数作为响应完成时将调用的最后一个参数传递。该函数是您的代码恢复的地方。
pdfToImgExec(..., ..., ..., ..., function(data) {
// resume your code here.
alert(data);
})