weixin_33725239 2014-11-21 12:47 采纳率: 0%
浏览 27

Ajax中的Ajax调用

I have a button on my webpage. If a user clicks it, a task starts and returns a ID. I want to use this ID to check the progress of the task in another ajax call, to let the user know if the task was completed.

I have tried this, but I don't think it is the correct way:

<script type="text/javascript">
$(document).ready(function() {
    $('#get-user-transactions').click(function() {

        $( "#user-transactions" ).after( "<p>Loading data</p>" );

            $.ajax({
                url: "/start-task/",
                dataType: "json", 
                success: function(data){ 
                    if(data) { 

                        var stateCheck = setInterval(   

                            $.ajax({
                                url: "/check-taskstate/"+data.task_id,
                                dataType: "json", 
                                success: function(data){ 

                                   if (data.updated == 'true') {
                                       clearInterval(stateCheck);
                                       window.location.reload();
                                   }
                                } 

                            })//ajax

                        , 1000); //stateCheck   


                    }                        

                } 

            })//ajax

    });
});
</script>

Edit

I updated my code, so the first url also gives a json response. But If I try my code I get this error:

Uncaught SyntaxError: Unexpected identifier

  • 写回答

1条回答 默认 最新

  • 乱世@小熊 2014-11-21 13:51
    关注

    Since setInterval() accepts a function or code but you are passing an jqXHR object you must be getting error for that.

    func is the function you want to be called repeatedly

    code in the alternate syntax, is a string of code you want to be executed repeatedly (using this syntax is not recommended for the same reasons as using eval())

    Use

    var stateCheck = setInterval(function(){
        $.ajax({
            url: "/check-taskstate/"+data.task_id,
            dataType: "json", 
            success: function(data){ 
    
               if (data.updated == 'true') {
                   clearInterval(stateCheck);
                   window.location.reload();
               }
            } 
    
        });
    }, 1000); //stateCheck 
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度