So, I’m trying the Ticker, Event Emitter Exercises from Hands On Node.js
I have this code:
var EventEmitter = require('events').EventEmitter,
util = require('util');
// Ticker Constructor
var Ticker = function (interval) {
this.interval = interval;
this.pulse = null;
}
util.inherits(Ticker, EventEmitter);
Ticker.prototype.start = function() {
this.emit('start');
this.tick();
}
Ticker.prototype.stop = function() {
if (this.pulse != null) clearTimeout(this.pulse);
this.emit('stop');
}
Ticker.prototype.tick = function() {
this.emit('tick');
this.pulse = setTimeout(this.tick, this.interval);
}
var ticker = new Ticker(1000);
ticker.on('start', function() { console.log('Ticker: Start'); });
ticker.on('tick', function() { console.log('Ticker: Tick'); });
ticker.on('stop', function() { console.log('Ticker: Stop'); });
ticker.start();
Which outputs the following, when run:
Ticker: Start
Ticker: Tick
timers.js:103
if (!process.listeners('uncaughtException').length) throw e;
^
TypeError: Object #<Object> has no method 'emit'
at Object.Ticker.tick [as _onTimeout] (/Users/twilson/Projects/tutorials/node/ticker-01.js:23:8)
at Timer.list.ontimeout (timers.js:101:19)
Where line ticker-01.js:23 is this.emit('tick'); of the Ticker.prototype.tick function.
Help, as I really can’t see what on earth is going wrong, bound to be a scoping thing no doubt? 🙁
When calling
setTimeout(this.tick, this.interval)thetickmethod will execute within the default context, not whatthisrefers to right there. You need to either…Bind the value of
thisto the ticker instance:Or save a reference to the ticker instance: