问题
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