希望得到一些帮助。我是 Nodejs 的新手,想知道是否可以删除这个自定义事件发射器。大部分代码来自 Pedro Teixeira 的 Hand on nodejs。我在底部的功能是尝试删除您在书中设置的自定义事件发射器。
var util = require('util');
var EventEmitter = require('events').EventEmitter;
// Pseudo-class named ticker that will self emit every 1 second.
var Ticker = function()
{
var self = this;
setInterval(function()
{
self.emit('tick');
}, 1000);
};
// Bind the new EventEmitter to the sudo class.
util.inherits(Ticker, EventEmitter);
// call and instance of the ticker class to get the first
// event started. Then let the event emitter run the infinante loop.
var ticker = new Ticker();
ticker.on('tick', function()
{
console.log('Tick');
});
(function tock()
{
setInterval(function()
{
console.log('Tock');
EventEmitter.removeListener('Ticker',function()
{
console.log("Clocks Dead!");
});
}, 5000);
})();