2014-11-20 101 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