setInterval внутри обьекта
Вопрос №1
Чего this._timerBeep() выполняется только один раз? var Module = sys.Class.extend({ _init: function() { console.log('_init!'); setInterval(this._timerBeep(), 1000); }, _timerBeep: function(){ console.log('_timerBeep!'); this.update(); this.draw(); }, update: function() { console.log('update!'); }, draw: function(){ console.log('draw!'); } }); Module2 = Module.extend(); new Module2(); Console log: _init! _timerBeep! update! draw! Вопрос №2 Как this передать в анонимную функцию...сделать что-то типа этого.. var Module = sys.Class.extend({ _init: function() { console.log('_init!'); setInterval(function(){ console.log('_timerBeep!'); this.update(); this.draw(); }, 1000); }, update: function() { console.log('update!'); }, draw: function(){ console.log('draw!'); } }); Module2 = Module.extend(); new Module2(); Console log: _init! _timerBeep! TypeError: this.update is not a function this.update(); _timerBeep! TypeError: this.update is not a function this.update(); _timerBeep! TypeError: this.update is not a function this.update(); |
Ответ на оба вопроса.
Заменить setInterval(this._timerBeep(), 1000); На setInterval(this._timerBeep.bind(this), 1000); |
Ты такой молодец :) спасибо тебе
|
уровень безграмотности на форуме просто зашкаливает.
народ даже справочник не читает по функциям. |
Часовой пояс GMT +3, время: 12:51. |