Skip to content

Instantly share code, notes, and snippets.

@joshsmith
Created November 21, 2011 20:30
Show Gist options
  • Save joshsmith/1383826 to your computer and use it in GitHub Desktop.
Save joshsmith/1383826 to your computer and use it in GitHub Desktop.
Hands-On Node Event Emitter Exercise 1
var EventEmitter = require('events').EventEmitter;
var util = require('util');
// Here is the Ticker constructor
var Ticker = function() { // This is a pseudo-class }
util.inherits(Ticker, EventEmitter);
// Emit tick on the tick event
Ticker.prototype.tick = function() {
this.emit('tick event', 'tick');
}
var ticker = new Ticker();
function tick() {
setTimeout(function() {
console.log('tick');
tick();
}, 1000);
}
ticker.on('tick event', tick);
ticker.tick();
@abi93k
Copy link

abi93k commented Mar 22, 2013

Correct me if I am wrong.

Aren't you supposed to call ticker.tick(); on line 19 ?

tick() will call the function directly and print tick to the console.

the ticker.tick() will emit the tick event which will in turn call the tick function.

@rudifa
Copy link

rudifa commented Apr 28, 2013

Hello Josh

var Ticker = function() { // This is a pseudo-class }

does not work, to fix it I moved the closing brace to the next line:

var Ticker = function() { // This is a pseudo-class 
}

Can you comment on this?
Rudi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment