码迷,mamicode.com
首页 > Web开发 > 详细

nodejs events模块

时间:2016-01-12 13:19:48      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

var EventEmitter = require(‘events‘).EventEmitter;

var emitter = new EventEmitter();

console.log(emitter.getMaxListeners());
//设置可监听事件的最大个数
emitter.setMaxListeners(11);

function work(who){
    console.log(who + ‘ go to work‘)
}

//监听事件
emitter.on(‘do‘, work);
emitter.on(‘do‘, function(who) {
    console.log(who + ‘ wash clothers‘)
});

emitter.on(‘rest‘, function(who) {
    console.log(who + ‘ can have a rest‘);
})

//移除监听事件
emitter.removeListener(‘do‘, work);

//触发事件
emitter.emit(‘do‘, ‘John‘);
emitter.emit(‘do‘, ‘lily‘);
emitter.emit(‘rest‘, ‘Tom‘);

//所有‘do’的监听事件
console.log(emitter.listeners(‘do‘))

//监听事件的个数
console.log(emitter.listenerCount(‘do‘));

 

nodejs events模块

标签:

原文地址:http://www.cnblogs.com/tianxintian22/p/5123870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!