[b]方法说明:[/b]
为指定事件注册一个 单次 监听器,所以监听器至多只会触发一次,触发后立即解除该监听器。
[b]语法:[/b]
emitter.once(event, listener)
[b]接收参数:[/b]
event (string) 事件类型
listener (function) 触发事件时的回调函数
[b]例子:[/b]
server.once('connection', function (stream) {
console.log('Ah, we have our first user!');
});
[b]源码:[/b]
EventEmitter.prototype.once = function(type, listener) {
if (!util.isFunction(listener))
throw TypeError('listener must be a function');
function g() {
this.removeListener(type, g);
listener.apply(this, arguments);
}
g.listener = listener;
this.on(type, g);
return this;
};