希望得到一些幫助。我是Nodejs的新手,想知道是否可以刪除這個自定義事件發射器。大部分代碼來自Pedro Teixeira的Hand on nodejs。我的底部功能是試圖刪除您在本書中設置的自定義事件發射器。的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);
})();