2014-02-08 52 views
0

我有2個JS類:jQuery的AJAX長輪詢不會中止

第一招:

var class1 = function() { 
    this.obj = new class2 

    this.stopLongPollAjax = function() { 
     obj.abort = true; 
     obj.ajax.abort(); 
     delete obj; // it's important part for me 
    } 
    var self = this; 
} 

第二個:

var class2 = function() { 
    this.abort = false; 
    this.ajax = null; 

    this.init = function() { 
     self.longpoll(); 
    } 

    this.longpoll = function() { 
     if(!self.abort) { 
      self.ajax = $.ajax({ 
       type: 'POST', 
       url: //url here 
       complete: function() { 
        self.longpoll(); 
       }, 
       success: function(response) { 
        if(response == 1) { 
         // callback 
        }   
      }    
     }); 
    } 
    var self = this; 
    this.init(); 
} 

當我打電話stopLongpoll方法來自class1longpoll不會形成另一個循環(當它完成時),但它在撥打stopLongpoll方法後不立即停止。有任何想法嗎? PS:如果我的代碼不容易閱讀或理解,我可以解釋它。

回答

0

的錯誤是非常愚蠢的......在class1我忘了把self操作之前obj。應該是這樣的:

obj.abort = true; 
obj.ajax.abort(); 
delete obj;