setInterval()
関数を使用してオブジェクト内のメソッドを使用してサーバーをポーリングしようとしています。コードは次のようになります。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()を2回呼び出してからもう一度呼び出します。私がどこに間違っているのか分かりません。
オブジェクト内のメソッドを一定の間隔で繰り返し呼び出すにはどうすればよいですか?
更新されたコード:
(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();
})();
関数リファレンスと関数呼び出しの違いは何ですか? – Teemu