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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Josh
does not work, to fix it I moved the closing brace to the next line:
Can you comment on this?
Rudi