emitter.removeListener(eventName, listener)
eventName
<string> | <symbol>listener
<Function>- 返回: <EventEmitter>
从名为 eventName
的事件的监听器数组中移除指定的 listener
。
const callback = (stream) => {
console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);
removeListener()
最多从监听器数组中删除一个监听器实例。
如果任何单个监听器已多次添加到指定 eventName
的监听器数组中,则必须多次调用 removeListener()
以删除每个实例。
这意味着在触发之后和最后一个监听器完成执行之前的任何 removeListener()
或 removeAllListeners()
调用都不会从正在进行的 emit()
中删除它们。
const myEmitter = new MyEmitter();
const callbackA = () => {
console.log('A');
myEmitter.removeListener('event', callbackB);
};
const callbackB = () => {
console.log('B');
};
myEmitter.on('event', callbackA);
myEmitter.on('event', callbackB);
// callbackA 删除监听器 callbackB,但它仍将被调用。
// 触发时的内部监听器数组 [callbackA, callbackB]
myEmitter.emit('event');
// 打印:
// A
// B
// callbackB 现在被删除了。
// 内部监听器数组 [callbackA]
myEmitter.emit('event');
// 打印:
// A
由于使用内部数组管理监听器,因此调用此方法将更改在移除监听器后注册的任何监听器的位置索引。
这不会影响调用监听器的顺序,但这意味着需要重新创建 emitter.listeners()
方法返回的监听器数组的任何副本。
当单个函数被多次添加为单个事件的句柄时(如下例所示),则 removeListener()
将删除最近添加的实例。
在示例中,删除了 once('ping')
监听器:
const ee = new EventEmitter();
function pong() {
console.log('pong');
}
ee.on('ping', pong);
ee.once('ping', pong);
ee.removeListener('ping', pong);
ee.emit('ping');
ee.emit('ping');
返回对 EventEmitter
的引用,以便可以链式调用。
eventName
<string> | <symbol>listener
<Function>- Returns: <EventEmitter>
Removes the specified listener
from the listener array for the event named
eventName
.
const callback = (stream) => {
console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);
removeListener()
will remove, at most, one instance of a listener from the
listener array. If any single listener has been added multiple times to the
listener array for the specified eventName
, then removeListener()
must be
called multiple times to remove each instance.
Once an event has been emitted, all listeners attached to it at the
time of emitting will be called in order. This implies that any
removeListener()
or removeAllListeners()
calls after emitting and
before the last listener finishes execution will not remove them from
emit()
in progress. Subsequent events will behave as expected.
const myEmitter = new MyEmitter();
const callbackA = () => {
console.log('A');
myEmitter.removeListener('event', callbackB);
};
const callbackB = () => {
console.log('B');
};
myEmitter.on('event', callbackA);
myEmitter.on('event', callbackB);
// callbackA removes listener callbackB but it will still be called.
// Internal listener array at time of emit [callbackA, callbackB]
myEmitter.emit('event');
// Prints:
// A
// B
// callbackB is now removed.
// Internal listener array [callbackA]
myEmitter.emit('event');
// Prints:
// A
Because listeners are managed using an internal array, calling this will
change the position indices of any listener registered after the listener
being removed. This will not impact the order in which listeners are called,
but it means that any copies of the listener array as returned by
the emitter.listeners()
method will need to be recreated.
When a single function has been added as a handler multiple times for a single
event (as in the example below), removeListener()
will remove the most
recently added instance. In the example the once('ping')
listener is removed:
const ee = new EventEmitter();
function pong() {
console.log('pong');
}
ee.on('ping', pong);
ee.once('ping', pong);
ee.removeListener('ping', pong);
ee.emit('ping');
ee.emit('ping');
Returns a reference to the EventEmitter
, so that calls can be chained.