Skip to content

Instantly share code, notes, and snippets.

@joshkehn
Forked from joshsmith/eventemitter.js
Created November 21, 2011 20:34
Show Gist options
  • Save joshkehn/1383843 to your computer and use it in GitHub Desktop.
Save joshkehn/1383843 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();
ticker.on('tick event', function () {
console.log('Caught tick event.');
});
setInterval(function () {
ticker.tick();
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment