問題描述
我有一個<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)
這篇關于如何刪除客戶端事件偵聽器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!