如何重复ajax请求取决于返回

时间:2016-11-21 20:27:34

标签: javascript jquery countdown jquery-countdown

我有这段代码:

.on('finish.countdown', function() {
                    var onEndAuction = function () {
                        $.ajax({
                            type: "POST",
                            url: "{{path('app_auction_end')}}",
                            data: {auctionId:{{ aReturn.oAuction.getId()}}},
                            success: function (data) {
                                console.log(data);
                                if (data == 0) {
                                    setTimeout(onEndAuction, i_timer);
                                } else {
                                    document.location.reload(true);
                                }
                            }
                        });
                    };
                });

我想如果data == 0需要在10秒后再次拨打app_auction_end。你能帮我吗 ? Thx提前和抱歉我的英语

2 个答案:

答案 0 :(得分:1)

为操作指定一个命名函数:

var someFunction = function () {
    $.ajax({
        //...
    });
};

然后您将用于.on()电话:

.on('finish.countdown', someFunction)

success处理程序中,为该函数设置超时:

if (data == 0) {
    setTimeout(someFunction, i_timer);
}

答案 1 :(得分:0)

.on('finish.countdown', function() {
  var onEndAuction = function () {
    $.ajax({
      type: "POST",
      url: "{{path('app_auction_end')}}",
      data: {auctionId:{{ aReturn.oAuction.getId()}}},
      success: function (data) {
        console.log(data);
        if (data == 0) {
          setTimeout(onEndAuction, i_timer);
        } else {
          document.location.reload(true);
        }
      }
    });
  };
  //do our initial call otherwise it will never get called.
  onEndAuction();
});