Javascript 使2个AJAX jquery同步工作
我需要管理2个AJAX jQuery函数,以便在服务器中启动一个进程并从服务器获取状态 一、 e。第一个AJAX JQuery将向服务器发送启动特定进程的请求,并返回到客户端,状态为“ProcessRunning”。第二个AJAX JQuery函数将在第一个函数的成功块内调用,它负责查询状态,直到得到响应“processcomplete” 这是我的psedo代码Javascript 使2个AJAX jquery同步工作,javascript,jquery,ajax,Javascript,Jquery,Ajax,我需要管理2个AJAX jQuery函数,以便在服务器中启动一个进程并从服务器获取状态 一、 e。第一个AJAX JQuery将向服务器发送启动特定进程的请求,并返回到客户端,状态为“ProcessRunning”。第二个AJAX JQuery函数将在第一个函数的成功块内调用,它负责查询状态,直到得到响应“processcomplete” 这是我的psedo代码 //This function which will be called on a button click function bui
//This function which will be called on a button click
function buildApplication(){
//show the gif spinner in the front-end
$.when(buildApp()).then(function(result){
console.log('process-completed');
//hide the gif spinner in the front-end
});
}
function buildApp(){
return $.ajax({
type: 'POST',
url: 'url pointing to php script',
data: dataString,
cache: false,
success: function(result){
if(result == 'process-running'){
console.log('monitor starts');
getAppStatus();
console.log('monitor ends');
}
},
error: function(result){
}
});
}
function getAppStatus(){
console.log('Querying server');
return $.ajax({
type: 'POST',
url: 'url pointing to php script',
data: dataString,
cache: false,
success: function(result){
if(result == 'process-running'){
getAppStatus();
}
},
error: function(result){
}
});
}
休眠过程在getAppStatus的PHP脚本中处理。也就是说,如果进程仍在运行,那么服务器将休眠10秒,然后只向客户端返回响应
问题是这两个函数都是异步运行的。这意味着buildApp函数调用getAppStatus函数并立即返回。getAppStatus函数作为孤立进程运行
如何使其同步(或类似),以便父调用方将等待子调用方返回
注意:我在getAppStatus函数中尝试了
async:false
,但它冻结了浏览器,ajax加载程序图像停止旋转,看起来像挂起了一样。我建议您使用Promission来管理它。@cgatian不幸的是,我们无法使用它,因为Android浏览器不支持该Promission。任何其他选择……如果您使用jQuery,它包括承诺