Home >Web Front-end >JS Tutorial >Custom event examples in Nodejs_node.js
In fact, just inherit the EventEmitter of events, and then you can register the event through on; emit to trigger the event, and removeListener to remove the event. A simple example is as follows:
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"); });
In this way, the customized Ticker has the ability to customize events