Home > Web Front-end > JS Tutorial > body text

Custom event examples in Nodejs_node.js

WBOY
Release: 2016-05-16 16:43:50
Original
1800 people have browsed it

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");
});
Copy after login

In this way, the customized Ticker has the ability to customize events

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template