首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jquery进行同步调用并执行

jquery进行同步调用并执行
EN

Stack Overflow用户
提问于 2016-05-17 19:21:56
回答 2查看 66关注 0票数 0

在下面的代码中,调用了execute_wc接口,该接口依次调用递归函数poll_results,并且仅在状态为!= 1时返回。现在,当调用函数poll_results时,它是异步的,因此submit_stat中的if else条件不是executed.how来处理此条件

代码语言:javascript
复制
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:

代码语言:javascript
复制
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"
     );
EN

回答 2

Stack Overflow用户

发布于 2016-05-17 19:27:29

您可以使用回调函数。下面的例子

代码语言:javascript
复制
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
    } 
   });
 }
票数 0
EN

Stack Overflow用户

发布于 2016-05-17 19:29:25

为了异步接收数据,您需要利用回调。向poll_results添加一个函数,当异步调用完成时,数据将被传递到。

代码语言:javascript
复制
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);
    }
   });
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37274681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档