Stop loop in discord.js

╄→尐↘猪︶ㄣ 提交于 2019-12-13 23:05:00

问题


I need help to stop a loop in discord.js. I don't know what to do to stop the loop.

let sec = 5;
let timer = setInterval(function() {
  if (command = "8") message.channel.send("ddd");
}, sec * 1000);

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

My problem is that clearInterval is not defined.


回答1:


In if (condition) statements, you can't use =. This is used to assign a new value to a variable, property or anything else.

The is equal to selectors are:

  • == for equal to.
  • === for equal to of the same type.
1 == '1' // returns true
1 === '1' // returns false



回答2:


client.on ('message', async message => {
  var prefix = "=",
      command = message.content.slice (prefix.length).split (" ")[0],
      sec = 5;
  switch (command) {
    case "startloop":
      if (message.channel.loop) return message.channel.send ('loop is already started');
      else message.channel.loop = setInterval (() => message.channel.send ('ddd'), sec * 1000);
      break;
    case "stoploop":
      if (!message.channel.loop) return message.channel.send ('no loop to stop lol');
      else {
        clearInterval (message.channel.loop);
        message.channel.loop = false;
      }
      break;
  }
});


来源:https://stackoverflow.com/questions/54650583/stop-loop-in-discord-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!