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