Home  >  Article  >  Web Front-end  >  Custom event examples in Nodejs_node.js

Custom event examples in Nodejs_node.js

WBOY
WBOYOriginal
2016-05-16 16:43:501801browse

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn