emitter.once(eventName, listener)
-
listener
<Function> 回调函数¥
listener
<Function> The callback function -
¥Returns: <EventEmitter>
为名为 eventName
的事件添加一次性 listener
函数。下次触发 eventName
时,将移除此监听器,然后再调用。
¥Adds a one-time listener
function for the event named eventName
. The
next time eventName
is triggered, this listener is removed and then invoked.
server.once('connection', (stream) => {
console.log('Ah, we have our first user!');
});
返回对 EventEmitter
的引用,以便可以链式调用。
¥Returns a reference to the EventEmitter
, so that calls can be chained.
默认情况下,事件监听器按添加顺序调用。emitter.prependOnceListener()
方法可用作将事件监听器添加到监听器数组开头的替代方法。
¥By default, event listeners are invoked in the order they are added. The
emitter.prependOnceListener()
method can be used as an alternative to add the
event listener to the beginning of the listeners array.
const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
// b
// a