Instructions on the use of the events.emb. once method in node.js
- 2020-03-30 04:35:52
- OfStack
Method description:
Registers a single listener for the specified event, so that the listener fires only once at most, and is immediately unfired.
Grammar:
emitter.once(event, listener)
Receiving parameters:
Event (string) The event type
Listener (function) The callback function when an event is triggered
Example:
server.once('connection', function (stream) {
console.log('Ah, we have our first user!');
});
Source:
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;
};