This repository has been archived on 2026-03-31. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
jsmag/events/listing_2.js
Ryan McGrath 5ed915c6c5 Initial
2011-02-18 04:25:06 -05:00

20 lines
437 B
JavaScript

var events = require('events'),
util = require('util');
var Foo = function(initial_no) { this.count = initial_no; };
Foo.prototype = new events.EventEmitter;
Foo.prototype.increment = function() {
var self = this;
setInterval(function() {
if(self.count % 2 === 0) self.emit('even');
self.count++;
}, 300);
};
var lol = new Foo(1);
lol.on('even', function() {
util.puts('Number is even! :: ' + this.count);
}).increment();