首页 > 解决方案 > 我需要在 discord.js 中停止循环的示例

问题描述

我想完成循环或限制消息。

我不知道该怎么做才能停止循环。请帮忙

例如我想做

let timer = setInterval(function(){    
if(command = "loop")
message.channel.send("test")
       }, sec * 1000)
if(command = "stoploop")
//i need example to stopping the loop

标签: discord.js

解决方案


使用clearInterval()停止运行间隔。

let timer = setInterval(function() {
  if(command === "loop")
    message.channel.send("test")
}, sec * 1000)

if(command === "stoploop") {
  clearInterval(timer);
}

请记住,每秒向频道发送一条消息是对 Discord API 的滥用。长时间执行此类行为可能会使 Discord 由于 API 滥用而禁止您的机器人。


推荐阅读