問(wèn)題描述
我有一個(gè)<Client>.on
函數(shù),我想停止監(jiān)聽這個(gè)事件,例如:
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?
推薦答案
我假設(shè) bot
是 Client
,它擴(kuò)展了 Node.js 的 EventEmitter
.您可以使用 message 或 ready
)="nofollow noreferrer">on
并使用 關(guān)閉
.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.
將監(jiān)聽器函數(shù)存儲(chǔ)在一個(gè)變量中,當(dāng)你需要移除那個(gè)監(jiān)聽器時(shí)使用 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)
這篇關(guān)于如何刪除客戶端事件偵聽器?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!