How to remove a Client event listener?(如何删除客户端事件侦听器?)
问题描述
我有一个<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)
这篇关于如何删除客户端事件侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何删除客户端事件侦听器?


基础教程推荐
- Chart.js 在线性图表上拖动点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01