如何取消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);
})
};
我已經在這上好幾個小時了。由於
'stats_url'不會出現在'網址進行更新:stats_url'時'get_live_stats(圓形)'叫什麼名字? – guest271314 2014-11-20 18:43:20
我不明白你的問題。我已經設置了stats_url – justcode 2014-11-20 18:46:53
如果'settings'對象緩存了以前的請求 - 包括'url'屬性後面的'data',那麼不確定嗎?嘗試添加'$ .ajaxSetup({beforeSend:function(jqxhr,settings){console.log(settings.data,settings.url)}})'查看數據''url是否與之前的請求相同? – guest271314 2014-11-20 19:09:32