Home > Web Front-end > JS Tutorial > Instructions for using the events.emitter.once method in node.js_node.js

Instructions for using the events.emitter.once method in node.js_node.js

WBOY
Release: 2016-05-16 16:27:52
Original
1645 people have browsed it

Method description:

Register a single listener for the specified event, so the listener will only trigger once at most, and the listener will be released immediately after triggering.

Grammar:

Copy code The code is as follows:

emitter.once(event, listener)

Receive parameters:

event (string) Event type

listener (function) The callback function when an event is triggered

Example:

Copy code The code is as follows:

server.once('connection', function (stream) {
console.log('Ah, we have our first user!');
});

Source code:

Copy code The code 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;
};
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template