我对异步和同步ajax调用的想法感到困惑。是否可以异步调用一个ajax调用,然后进行同步调用?
这是我的场景, 我正在尝试创建一个实时进度条,以显示没有插入数据,其中postdata()将数据插入表中,而getprocess()函数返回当前没有插入的数据以显示在进度条中。
Reference:: making progress bar
function getprogress(data){
console.log(data);
$.ajax({
method: 'POST',
url: '<?php echo base_url()?>setting/processoffline/getprogressdata',
data:{ table:data },
dataType: 'json',
async: false,
success: function(val) {
var off=parseFloat(Number(val.dataoffline),10);
var on=parseFloat(Number(val.dataonline),10);
var percent=Math.round((off/on)*100);
// $('#'+data+'success').addClass('hidden');
console.log('offline- '+off);
console.log('online- '+on);
$('#'+data+'progressbar').css('width',percent+"%");
$('#'+data+'progressbar').html(percent+"%" + off+' out of '+on );
if(percent=='100'){
console.log(percent);
// $('#'+data+'progressbox').addClass('hidden');
$('#'+data+'success').removeClass('hidden');
$('.download').removeAttr('disabled','disabled');
// clearTimeout();
}
console.log(postComplete);
if (!postComplete)
setTimeout( function() { getprogress(data); }, 1500);
} ,
error: function() {
if (!postComplete){
setTimeout( function() { getprogress(data); }, 1500);
}
}
});
}
function postdata(data)
{
// if(!data)
$.ajax({
method: 'POST',
url: '<?php echo base_url()?>setting/processoffline/processdata',
data:{ master_table:data },
dataType: 'html',
success: function() {
postComplete = true;
},
error: function() {
postComplete = true;
}
});
}
现在我在这里调用这些函数
$(this).parent('td').parent('tr').siblings('tr').children('td').find('input:checked').each(function(){
data=$(this).attr('id');
postdata(data);
$('#'+data+'success').addClass('hidden');
$('#'+data+'progressbox').removeClass('hidden');
i++;
getprogress(data);
if(!postComplete)
setTimeout( function() { getprogress(data);}, 2);
这里postdata()函数在循环中多次被异步调用我的情况是,因为循环可以是无限数量,这可以启动很多可以挂起我的系统的并行进程。我需要为postdata启动下一个循环()只有当前面的进程结束时.Function postdata()对php函数进行ajax调用,这需要很多时间。所以我的技术是异步定义postdata()函数,这样我可以并行调用getprogress()函数但是(我不知道是否可能)我想同步调用getprocess ajax调用,这样循环将等到getprogress返回的值结束。这样我只有在早期进程结束时才能在ajax调用中启动一个新进程。 / p>
我想知道它是否有可能,如果不能,我怎么能解决这个问题。对于糟糕的英语和如果不清楚请注释,我坚持3-4天。 提前致谢