使用setInterval调用对象内部的方法不起作用

时间:2017-02-22 16:00:55

标签: javascript jquery

我正在尝试使用setInterval()函数使用对象内部的方法轮询我的服务器。代码如下所示:

(function(){

    var teacherZone = {

        init: function(){
            this.cacheDOM();
            this.bindEvents();
            this.pollServer(); // get the list of lessons
        },
        cacheDOM: function(){
            this.$showLessons     = $('#showLessons');
            this.$lessonsTemplate = $('#lessonsTemplate');
        },
        bindEvents: function(){

        },
        pollServer: function(){
            alert('Polled');
        }

    }

    teacherZone.init();
    setInterval(teacherZone.pollServer(), 1000)
})();

这会调用alert()两次但不再调用。不知道我在哪里错了。

如何以设定的间隔重复调用对象内的方法?

更新后的代码:

(function(){

    var teacherZone = {

        interval: 5000,

        init: function(){
            this.cacheDOM();
            this.bindEvents();
            setInterval(function(){
                teacherZone.pollServer(); // get the list of lessons
            }, teacherZone.interval);
        },
        cacheDOM: function(){
            this.$showLessons     = $('#showLessons');
            this.$lessonsTemplate = $('#lessonsTemplate');
        },
        bindEvents: function(){

        },
        pollServer: function(){
            alert('Polled');
            // data = {
            //     name: 'John'
            // };
            // this.$showLessons.html(Mustache.render(this.$lessonsTemplate.html(), data));
        }

    }

    teacherZone.init();
    teacherZone.pollServer();

})();

4 个答案:

答案 0 :(得分:4)

您需要删除括号,因为您立即调用该函数并且不要移交该函数。

setInterval(teacherZone.pollServer, 1000)
//                               ^^

答案 1 :(得分:2)

您需要一个函数引用而不是调用函数。

更改 来自: setInterval(teacherZone.pollServer(), 1000)
至" setInterval(teacherZone.pollServer, 1000)



(function(){

    var teacherZone = {

        init: function(){
            this.cacheDOM();
            this.bindEvents();
            this.pollServer(); // get the list of lessons
        },
        cacheDOM: function(){
            this.$showLessons     = $('#showLessons');
            this.$lessonsTemplate = $('#lessonsTemplate');
        },
        bindEvents: function(){

        },
        pollServer: function(){
            alert('Polled');
        }

    }

    teacherZone.init();
    setInterval(teacherZone.pollServer, 1000)
})();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您也可以尝试这种方式:

(function(){ 
   $.teacherZone = { 
    init: function(){
        this.cacheDOM();
        this.bindEvents();
        this.pollServer(); 
    },
    cacheDOM: function(){
        this.$showLessons     = $('#showLessons');
        this.$lessonsTemplate = $('#lessonsTemplate');
    },
    bindEvents: function(){

    },
    pollServer: function(){
        console.log('Polled');
    } 
  } 
 $.teacherZone.init(); 
 setInterval($.teacherZone.pollServer, 1000);

})(); 

答案 3 :(得分:1)

您必须将对该函数的引用传递给select [Int Field] , '''' + convert(varchar(10),[Int field]) + ''',' as clientfield , '''' + Str([Int field],len([Int field])) + ''',' as clientfield2 ... ,如下所示:

setInterval

如果您希望能够在setInterval(teacherZone.pollServer, 1000); 函数中使用this,那么您也可以使用{{3}将pollServer绑定到您的对象像这样:

this

或将对setInterval(teacherZone.pollServer.bind(teacherZone), 1000); 的调用包装在一个像这样的无限函数中:

pollServer

因为如果你不这样做,setInterval(function() { teacherZone.pollServer(); }, 1000); 将成为this setInterval对象的父对象。

修改

您可以通过简单的方式执行此操作,以防您必须创建更多window实例或重命名对象teacherZone,您不需要重命名依赖于它的所有行。只需将teacherZone的值存储在this之外(阅读关闭),如下所示:

setInterval

现在您可以重命名对象init: function(){ this.cacheDOM(); this.bindEvents(); var that = this; // store this here so it will be accessible inside setInterval setInterval(function(){ that.pollServer(); // use that here (because this will be the window object here so we use that because it holds a reference to the object we want) }, this.interval); // no need to use that here just this will suffice (because here we are inside init not inside the callback of setInterval) }, 而无需担心!