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


基础教程推荐
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01