Javascript 对于循环,仅当内部函数完成时才继续循环?
下面是一个函数,首先获取一个文件列表,对于每个文件,它将调用一个ajax函数来执行以下操作:Javascript 对于循环,仅当内部函数完成时才继续循环?,javascript,jquery,ajax,asynchronous,synchronization,Javascript,Jquery,Ajax,Asynchronous,Synchronization,下面是一个函数,首先获取一个文件列表,对于每个文件,它将调用一个ajax函数来执行以下操作: function getImagesList(folder) { $.ajax({ type:"POST", url: "getImgList.php", data: {"data":folder}, dataType: "json", success: fun
function getImagesList(folder) {
$.ajax({
type:"POST",
url: "getImgList.php",
data: {"data":folder},
dataType: "json",
success: function (data) {
var IsfirstLoggingItem = "true";
for (var key in data) {
//alert (data[key]);
pdfToImgExec (data[key],IsfirstLoggingItem,folder,1);
$('#pdfToImgResult').append('<p>Processing ' + data[key] + ' <img src="loading.gif" height="20" width="20"/></p>');
IsfirstLoggingItem = "false";
}
folder == 'Incoming' ? getImagesList('result') : pdfToImgResult();
},
error: function(x, t, m) {
$('#pdfToImgResult').html('<p>Error</p>');
alert (t);
releaseBtn();
}
});
}
函数getImagesList(文件夹){
$.ajax({
类型:“POST”,
url:“getImgList.php”,
数据:{“数据”:文件夹},
数据类型:“json”,
成功:功能(数据){
var IsfirstLoggingItem=“true”;
for(var输入数据){
//警报(数据[键]);
pdfToImgExec(数据[键],IsfirstLoggingItem,文件夹,1);
$('#pdfToImgResult')。追加('处理'+data[key]+'');
IsfirstLoggingItem=“false”;
}
文件夹==“传入”?getImagesList(“结果”):pdfToImgResult();
},
错误:函数(x,t,m){
$('pdfToImgResult').html('p>错误');
警报(t);
releaseBtn();
}
});
}
这是被调用方函数,它包含一个ajax函数来执行某些操作
function pdfToImgExec(file,IsfirstLogging,folder,round){
var postString = file + '&' + IsfirstLogging + '&' + folder;
$.ajax({
type: "POST",
url: "pdfToImgExec.php",
data: {"data":postString},
dataType: "html",
success: function (data) {
if (data) {
$('#pdfToImgResult').html('').append('<p>Finish processing ' + file + '</p>');
} else if (!data && round < 4) {
$('#pdfToImgResult').html('').append('<p>Encounter error in processing ' + file + ' , retrying ' + round + ' round </p>');
round++;
pdfToImgExec(file,IsfirstLogging,folder,round);
}
},
error: function(x, t, m) {
$('#pdfToImgResult').html('errpr');
alert (t);
releaseBtn();
}
});
}
函数pdfToImgExec(文件、IsfirstLogging、文件夹、循环){
var postString=file+'&'+IsfirstLogging+'&'+文件夹;
$.ajax({
类型:“POST”,
url:“pdfToImgExec.php”,
数据:{“数据”:postString},
数据类型:“html”,
成功:功能(数据){
如果(数据){
$('pdfToImgResult').html('').append('p>finishprocessing'+file+'');
}如果(!数据和舍入<4),则为else{
$('pdfToImgResult').html('').append('p>处理'+file+'时遇到错误,重试'+round+'round');
round++;
pdfToImgExec(文件,IsfirstLogging,文件夹,循环);
}
},
错误:函数(x,t,m){
$('pdfToImgResult').html('errpr');
警报(t);
releaseBtn();
}
});
}
简而言之,getImagesList函数获取一个文件列表,对于每个文件,调用pdfToImgExec函数来执行一些ajax操作。问题是,它不会等待ajax人员完成,而是开始下一个循环
这意味着,我想要例如file1运行ajax staff=>file1完成ajax staff=>file2运行ajax staff=>file2完成=>检查结果
但是,当前的情况是file1运行ajax staff=>file2运行ajax staff=>check result=>file1 finish/file2 finish
我在这两个函数中都尝试了async:false和cache:false,但似乎没有任何帮助,如何修复它?谢谢使用数组存储数据
var dataArr=[];
在第一个函数中,将数据推送到数组
function getImagesList(folder) {
$.ajax({
type:"POST",
url: "getImgList.php",
data: {"data":folder},
dataType: "json",
success: function (data) {
dataArr = data;
doProcessData();
},
error: function(x, t, m) {
$('#pdfToImgResult').html('<p>Error</p>');
alert (t);
releaseBtn();
}
});
}
希望这有帮助 您可以将
getImgList.php
中的回调设置为基于setInterval的函数,当pdfToImgExec.php
完成时,该函数将循环。这些是相同的后端吗?如果这两个操作之间有如此紧密的关联,为什么后端不处理这两个操作?对不起,你的意思是后端可以处理执行顺序而不需要任何php代码吗?我给了你两个选项。第一个选项是通过setInterval
对循环进行“监控”来控制循环的前端方法。关于第二个选项。。。如果您控制后端,那么我将向一个执行这两个步骤的后端服务发出一个AJAX请求。谢谢,我想知道一个php文件是否包含“exec(“…”)”,AJAX调用是在exec完成后执行success function,还是只在调用了“exec”命令后执行success function?对不起,我不知道php:)
function doProcessData(itemDoneCallback, allDoneCallback) {
if(typeof(itemDoneCallback)==undefined)itemDoneCallback=function(){};
if(typeof(allDoneCallback)==undefined)allDoneCallback=function(){};
if(arrData.length>0){
var key = arrData[0];
$.ajax({
//option here like your function pdfToImgExec
success: function (data) {
arrData.splice(0,1);//remove first data.
doProcessData();// call to process next data.
itemDoneCallback(); // callback to process some thing
}
})
}else{
allDoneCallback();// all done callback
}
}