Created
November 21, 2011 20:30
-
-
Save joshsmith/1383826 to your computer and use it in GitHub Desktop.
Hands-On Node Event Emitter Exercise 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
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.
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
I'd like to see a fork of this that tackles Exercise 2: "Build a script that instantiates one Ticker and bind to the "tick" event, printing "TICK" every time it gets one."