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

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

WBOY
Release: 2016-05-16 16:27:55
Original
1687 people have browsed it

Method description:

Remove a listener for the specified event.

Grammar:

Copy code The code is as follows:

emitter.removeListener(event, listener)

Receive parameters:

event (string) Event type

listener (function) Registered listener

Example:

Copy code The code is as follows:

var callback = function(stream) {
console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);

Source code:

Copy code The code is as follows:

// emits a 'removeListener' event iff the listener was removed
EventEmitter.prototype.removeListener = function(type, listener) {
var list, position, length, i;
if (!util.isFunction(listener))
Throw TypeError('listener must be a function');
if (!this._events || !this._events[type])
Return this;
list = this._events[type];
length = list.length;
position = -1;
if (list === listener ||
(util.isFunction(list.listener) && list.listener === listener)) {
Delete this._events[type];
If (this._events.removeListener)
This.emit('removeListener', type, listener);
} else if (util.isObject(list)) {
for (i = length; i-- > 0;) {
If (list[i] === listener ||
(list[i].listener && list[i].listener === listener)) {
position = i;
         break;
}
}
If (position < 0)
        return this;
If (list.length === 1) {
List.length = 0;
Delete this._events[type];
} else {
List.splice(position, 1);
}
If (this._events.removeListener)
This.emit('removeListener', type, listener);
}
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