问题描述
我有一个<Client>.on
函数,我想停止监听这个事件,例如:
I have a <Client>.on
function and I want to stop the listening of this event, example:
bot.on('messageCreate', message => {
// some code
});
如何停止它并且不再接收此事件?
How to stop it and not receive this event anymore?
推荐答案
我假设 bot
是 Client
,它扩展了 Node.js 的 EventEmitter
.您可以使用 message 或 ready
)="nofollow noreferrer">on
并使用 关闭
.addListener
和 removeListener
也分别是 on
和 off
的别名.
I'm assuming bot
is an instance of Client
, which extends Node.js' EventEmitter
. You can listen to an event (for example message
or ready
) using on
and remove that listener using off
. addListener
and removeListener
are also aliases of on
and off
respectively.
将监听器函数存储在一个变量中,当你需要移除那个监听器时使用 bot.off(eventName, listener)
:
Store the listener function in a variable, and when you need to remove that listener use bot.off(eventName, listener)
:
const listener = () => {
// your code here...
}
bot.on('event', listener)
// When you need to remove the listener:
bot.off('event', listener)
这篇关于如何删除客户端事件侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!