Method description:
Register a single listener for the specified event, so the listener will only fire once at most, and will be immediately released after it is triggered.
grammar:
The code copy is as follows:
emitter.once(event, listener)
Receive parameters:
event (string) event type
listener (function) The callback function when the event is triggered
example:
The code copy is as follows:
server.once('connection', function (stream) {
console.log('Ah, we have our first user!');
});
Source code:
The code copy is as follows:
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;
};