2014-11-20 90 views
0

如何取消ajax請求,然後用新參數再次調用它?用我的代碼,以前的ajax請求仍然存在。中止長輪詢ajax請求並用新參數重新啓動請求

var stats_url = '/stats'; 
var live_stats_ajax_object = $.ajax(); 
$(".check-round").on("click", function(){ 
    live_stats_ajax_object.abort(); 
    round = $(this).attr('class').split(' ')[0]; 
    get_live_stats(round); 
}); 

var get_live_stats = function(round) { 
    live_stats_ajax_object = $.ajax({ 
     url: stats_url, 
     type: "GET", 
     data: 'live_stats=true' + "&event_id=" + $("#event-list option:selected").val() 
        + "&fight_id=" + $('input[name=fightlist]:checked').val() 
        + "&round=" + round, 
     dataType: "json", 
     timeout: 3500, 
     complete: function(xhr, textStatus) { 
      console.log("polling again stats for " + round);    
      if (textStatus != "abort") { 
       setTimeout(function() { get_live_stats(round); }, 10000); 
      } 
     },  
     success: function(data) { 
      console.log("polling and got live stats for " + round); 
      console.log(data); 
     }   
    }) 
     .fail(function() { 
      console.log("polling failed and couldn't get live stats for " + round); 
     }) 
}; 

我已經在這上好幾個小時了。由於

+0

'stats_url'不會出現在'網址進行更新:stats_url'時'get_live_stats(圓形)'叫什麼名字? – guest271314 2014-11-20 18:43:20

+0

我不明白你的問題。我已經設置了stats_url – justcode 2014-11-20 18:46:53

+0

如果'settings'對象緩存了以前的請求 - 包括'url'屬性後面的'data',那麼不確定嗎?嘗試添加'$ .ajaxSetup({beforeSend:function(jqxhr,settings){console.log(settings.data,settings.url)}})'查看數據''url是否與之前的請求相同? – guest271314 2014-11-20 19:09:32

回答

0

編輯,更新

嘗試

// create empty object 
var live_stats_ajax_object = {} 
// reference for `setTimeout` within `get_live_stats` 
, id = null 
, stats_url = "/stats" 
, get_live_stats = function (round) { 
     var eventlist = $("#event-list option:selected").val() 
     , fightlist = $('input[name=fightlist]:checked').val(); 
     live_stats_ajax_object = $.ajax({ 
      type: "GET", 
      url: stats_url, 
      data: "live_stats=true&event_id=" 
        + eventlist 
        + "&fight_id=" 
        + fightlist 
        + "&round=" 
        + round, 
      timeout: 3500 
     }); 
     // return `live_stats_ajax_object` 
     return live_stats_ajax_object 
     .done(function (data, textStatus, jqxhr) { 
      console.log("polling and got live stats for " + round + "\n" 
         , data);  
     }) 
     .fail(function (jqxhr, textStatus, errorThrown) { 
      console.log("polling failed and couldn't get live stats for " 
         + round); 
     }) 
     .always(function (jqxhr, textStatus) { 
      if (textStatus !== "abort") { 
       // set `id` to `setTimeout`'s `numerical ID` 
       // call `get_live_stats()` within `setTimeout` 
       id = setTimeout(function() { 
        get_live_stats(round); 
       }, 10000); 
      } 
     }); 
    }; 

$(".check-round").on("click", function() { 
    var round = $(this).attr('class').split(" ")[0]; 
    // at initial click `live_stats_ajax_object` is empty object , 
    // not having `jqxhr` `promise` property ; 
    // call `get_live_stats()` , which sets `live_stats_ajax_object` 
    // to `$.ajax()` , having `state` property at returned `jqxhr` object 
    if (!live_stats_ajax_object.hasOwnProperty("state")) { 
     get_live_stats(round); 
    } else { 
     // if `id` was set during initial call to `get_live_stats()` 
     if (id) { 
      // `.abort()` previous `live_stats_ajax_object` request , 
      // `clearTimeout()` of `id` , set `id` to `null` 
      // call `get_live_stats()` with current `round` argument 
      live_stats_ajax_object.abort(); 
      clearTimeout(id); 
      id = null; 
      get_live_stats(round); 
     } 
    } 
}); 

的jsfiddle http://jsfiddle.net/guest271314/7wrdo5wr/

+0

這個工程!請你能解釋你在這裏做什麼,以及請求不中止的問題是什麼?有沒有潛在的內存泄漏問題?謝謝。 – justcode 2014-11-21 14:00:18

+0

觀察完代碼後,我看到了我遇到的問題。即使我放棄了我的請求,但我只中止運行的請求,而不是等待超時的請求。失蹤的部分是明智的......再次感謝! – justcode 2014-11-21 19:02:13

+0

@justcode不客氣:)查看更新後的帖子。謝謝 – guest271314 2014-11-21 19:35:16