事件模型Events

1 程序1

var EventEmitter = require('events').EventEmitter
var myEmitter = new EventEmitter()
// addEventListener
myEmitter.on('get', function (something) {
    console.log(something + "1")
})
myEmitter.on('get', function (something) {
    console.log(something + "2")
})
myEmitter.on('get', function (something) {
    console.log(something + "3")
})
myEmitter.on('get', function (something) {
    console.log(something + "4")
})
myEmitter.on('get', function (something) {
    console.log(something + "5")
})
myEmitter.on('get', function (something) {
    console.log(something + "6")
})
myEmitter.on('get', function (something) {
    console.log(something + "7")
})
myEmitter.on('get', function (something) {
    console.log(something + "8")
})
myEmitter.on('get', function (something) {
    console.log(something + "9")
})
myEmitter.on('get', function (something) {
    console.log(something + "10")
})
myEmitter.emit("get", "监听")

结果:

监听1

监听2

监听3

监听4

监听5

监听6

监听7

监听8

监听9

监听10

如果继续增加第11条监听,系统警告:Warning: Possible EventEmitter memory leak detected. 11 get listeners added. Use emitter.setMaxListeners() to increase limit,即需要setMaxListeners()方法增加监听数量。但监听数太多容易造成内存泄漏

2 程序2

var EventEmitter = require('events').EventEmitter
var myEmitter = new EventEmitter()
// 设置最大监听数(默认建议10个监听)
myEmitter.setMaxListeners(11)
// addEventListener
myEmitter.on('get', function (something) {
    console.log(something + "1")
})
myEmitter.on('get', function (something) {
    console.log(something + "2")
})
myEmitter.on('get', function (something) {
    console.log(something + "3")
})
myEmitter.on('get', function (something) {
    console.log(something + "4")
})
myEmitter.on('get', function (something) {
    console.log(something + "5")
})
myEmitter.on('get', function (something) {
    console.log(something + "6")
})
myEmitter.on('get', function (something) {
    console.log(something + "7")
})
myEmitter.on('get', function (something) {
    console.log(something + "8")
})
myEmitter.on('get', function (something) {
    console.log(something + "9")
})
myEmitter.on('get', function (something) {
    console.log(something + "10")
})
myEmitter.on('get', function (something) {
    console.log(something + "11")
})
myEmitter.emit("get", "监听")

3 程序3 移除监听

var EventEmitter = require('events').EventEmitter
var myEmitter = new EventEmitter()
// 设置最大监听数(默认建议10个监听)
myEmitter.setMaxListeners(11)
// addEventListener
myEmitter.on('get', function (something) {
    console.log(something + "1")
})
myEmitter.on('get', function (something) {
    console.log(something + "2")
})
myEmitter.on('get', function (something) {
    console.log(something + "3")
})
myEmitter.on('get', function (something) {
    console.log(something + "4")
})
myEmitter.on('get', function (something) {
    console.log(something + "5")
})
myEmitter.on('get', function (something) {
    console.log(something + "6")
})
myEmitter.on('get', function (something) {
    console.log(something + "7")
})
myEmitter.on('get', function (something) {
    console.log(something + "8")
})
myEmitter.on('get', function (something) {
    console.log(something + "9")
})
myEmitter.on('get', function (something) {
    console.log(something + "10")
})
function getEmitter(something) {
    console.log(something + "11")
}
myEmitter.on('get', getEmitter)
// 移除监听
myEmitter.removeListener('get',getEmitter)
myEmitter.emit("get", "监听")

结果:移除了第11个监听

myEmitter.removeAllListeners('get')

结果:移除所有get监听事件

4 获取监听数

myEmitter.listeners('get').length
// 或者
EventEmitter.listenerCount(myEmitter,'get')

结果:10

转载于:https://my.oschina.net/iNiL0119/blog/680313

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值