new EventEmitter()
事件处理类
- Source
Example
const bus = new EventEmitter();
bus.addListener("timeout", (params) => {
console.log(1);
});
bus.addListener("timeout", function two(params) {
console.log(2);
bus.removeListener("timeout", two);
});
setInterval(() => {
bus.emit("timeout", Date.now());
}, 1000);
Methods
addListener(eventName, callback)
订阅
Parameters:
Name | Type | Description |
---|---|---|
eventName | String | 监听的事件名称 |
callback | function | 回调函数 |
- Source
emit(eventName) → {Boolean}
发布
Parameters:
Name | Type | Description |
---|---|---|
eventName | String | 发布的事件名称 |
- Source
Returns:
是否发布成功
- Type:
- Boolean
removeListener(eventName, callback) → {Boolean}
移除订阅
Parameters:
Name | Type | Description |
---|---|---|
eventName | String | 取消监听的事件名称 |
callback | function | 回调函数 |
- Source
Returns:
是否取消监听成功
- Type:
- Boolean