【问题标题】:jQuery AJAX Long Poll doesn't abortjQuery AJAX 长轮询不会中止
【发布时间】:2014-02-08 22:57:50
【问题描述】:

我有 2 个 JS 类:

第一个:

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();
}

当我从 class1 调用 stopLongpoll 方法时,longpoll 不会进行另一个循环(当它完成时),但它不会在调用 stopLongpoll 方法后立即停止。有任何想法吗?

PS:如果我的代码不容易阅读或理解,我可以解释一下。

【问题讨论】:

    标签: javascript jquery ajax long-polling


    【解决方案1】:

    这个错误非常愚蠢...在class1 中我忘记在obj 操作之前放置self.. 它应该看起来像:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多