Custom event instances in Nodejs

  • 2020-03-30 03:24:18
  • OfStack

So you can just inherit the EventEmitter of events, and then you can register events with on; Emit to trigger the event, removeListener to remove the event, simple example:


var util = require('util');
var Et = require('events').EventEmitter;
function Ticker() {
  var self = this;
  setInterval(function(){self.emit("tick")},1000);
}
util.inherits(Ticker,Et);
var ticker = new Ticker();
ticker.on("tick",function() {
  console.log("ticker");
});

This gives the custom Ticker the ability to customize events


Related articles: