在下面的代码中调用api execute_wc
,后者又调用递归函数poll_results
并仅在有状态时返回!= 1.现在函数poll_results
是称它是异步的,因此if else
中的submit_stat
条件不会被执行。如何处理这种情况
function submit_stat(){
var poll_status = '';
$.post("/reports/execute_wc/", snddata,
function callbackHandler(data, textstatus)
{
console.log('got response');
if (data.status == 0)
{
poll_status = poll_results();
if (poll_status == 1){
}
else if(poll_status == 0)
{
alert('Error while processing data.Please check input files');
}
}
else if (data.status == 1)
{
alert('Error while processing data');
}
},
"json"
);
}
function poll_results(){
$.post('/reports/poll_wcstatus/', function(data) {
if(data.wce_status == 1){
return setTimeout(poll_results,5000);
}
else if (data.wce_status != 1 || data.wce_status != 3)
{
alert('return 1');
return 1;
}
else
{
alert('return 0');
return 0;
}
});
}
编辑1:
function poll_results(callback){
var callback='';
$.post('/reports/poll_wcstatus/', function(data) {
//alert(data); // process results here
console.log(data);
console.log(data.wce_status);
if(data.wce_status == 1){
return setTimeout(poll_results,5000);
}
else if (data.wce_status != 1 || data.wce_status != 3){
alert('return 1');
//return 1;
callback(1);
}
else //3 is fatal error shoul
{
alert('return 0');
callback(0);
//return 0;
}
});
}
$.post("/reports/execute_wc/", snddata,
function callbackHandler(data, textstatus)
{
console.log('got response');
if (data.status == 0)
{
alert('starting script');
//poll_status = poll_results();
poll_results(function(poll_status){
if (poll_status == 1){
alert('Content extratcted.Please download the results file');
$('#loading').hide();
$('#downloadfile').show();
$('#downloadfile').html('').append('<a href="'+ data.url +'">Download output</a>');
}
else if(poll_status == 0)
{
$('#loading').hide();
alert('Error while processing data.Please check input files');
}
});
}
else if (data.status == 1)
{
$('#loading').hide();
alert('Error while processing data');
}
},
"json"
);
答案 0 :(得分:0)
您可以使用回拨功能。以下示例
function submit_stat(){
var poll_status = '';
$.post("/reports/execute_wc/", snddata,
function callbackHandler(data, textstatus)
{
console.log('got response');
if (data.status == 0)
{
poll_results(function(poll_status){ //pass a function definition as a parameter to your another function, this is called callback function
if (poll_status == 1){
}
else if(poll_status == 0)
{
alert('Error while processing data.Please check input files');
}
});
}
else if (data.status == 1)
{
alert('Error while processing data');
}
},
"json"
);
}
function poll_results(callback){ //here callback variable holds a function within it
$.post('/reports/poll_wcestatus/', function(data) {
if(data.wce_status == 1){
return setTimeout(poll_results,5000);
}
else if (data.wce_status != 1 || data.wce_status != 3)
{
alert('return 1');
callback(1); //execute the callback function
}
else
{
alert('return 0');
callback(0); //execute the callback function
}
});
}
答案 1 :(得分:0)
为了异步接收数据,您需要使用回调。向poll_results
添加一个函数,并在异步调用完成时传递数据。
function submit_stat(){
var poll_status = '';
$.post("/reports/execute_wc/", snddata,
function callbackHandler(data, textstatus)
{
console.log('got response');
if (data.status == 0)
{
poll_results(function(poll_status)
{
if (poll_status == 1){
}
else if(poll_status == 0)
{
alert('Error while processing data.Please check input files');
}
});
}
else if (data.status == 1)
{
alert('Error while processing data');
}
},
"json"
);
}
function poll_results(callback){
$.post('/reports/poll_wcstatus/', function(data) {
if(data.wce_status == 1){
return setTimeout(poll_results,5000);
}
else if (data.wce_status != 1 || data.wce_status != 3)
{
callback(1);
}
else
{
callback(0);
}
});
}