Javascript 非法的Break语句,但Break在while循环中

Javascript 非法的Break语句,但Break在while循环中,javascript,node.js,while-loop,break,Javascript,Node.js,While Loop,Break,我正在用discord.js创建一个discord机器人,当我添加了一个带有中断的while循环时,它会出现错误,出现“非法中断语句”。中断位于while循环中的if语句中。进行此操作时,需要一个while循环。代码如下: while(true){ const filter=m=>m.author.id==message.author.id; 设cword=rword(); 函数shuffle(){ var a=cword.split(“”); var n=a.长度; 对于(变量i=n-1;i>

我正在用discord.js创建一个discord机器人,当我添加了一个带有中断的while循环时,它会出现错误,出现“非法中断语句”。中断位于while循环中的if语句中。进行此操作时,需要一个while循环。代码如下:

while(true){
const filter=m=>m.author.id==message.author.id;
设cword=rword();
函数shuffle(){
var a=cword.split(“”);
var n=a.长度;
对于(变量i=n-1;i>0;i--){
var j=Math.floor(Math.random()*(i+1));
var tmp=a[i];
a[i]=a[j];
a[j]=tmp;
};
返回a.join(“”);
};
让aembed=newdiscord.MessageEmbed()
.setTitle(“新词”)
.setDescription(shuffle())
.setColor(“蓝色”)
.setFooter(“Word将在5分钟后过期”https://i.imgur.com/s7R7YC3.png")
.setTimestamp();
让rightembed=newdiscord.MessageEmbed()
.setTitle(“正确”)
.setDescription(“你有15个面包屑!请运行`.word`来获得一个新单词!”)
.setColor(“绿色”)
.setFooter(“橡皮鸭游戏”https://i.imgur.com/s7R7YC3.png")
.setTimestamp();
let ErrorEmbed=new Discord.MessageEmbed()
.setTitle(“错误”)
.setDescription(`正确的单词是**${cword.toLowerCase()}**。请运行\`.word\`获取新单词!`)
.setColor(“红色”)
.setFooter(“橡皮鸭游戏”https://i.imgur.com/s7R7YC3.png")
.setTimestamp();
message.channel.send(message.author{
嵌入:aembed
})。然后(p1=>{
让Word=new Discord.MessageCollector(p1.channel,filter{
最高:1,
时间:30万
});
on(“collect”,异步cmsg=>{
让WordMSG=cmsg.content.toLowerCase();
如果(WordMSG==cword.toLowerCase()){
单词。停止();
message.channel.send(rightembed);
db.add(`${message.author.id}.crumb`,15);
}否则{
单词。停止();
message.channel.send(错误嵌入);
break//这应该是为了打破循环,但它会出错
};
});
});
};

知道它有什么问题吗?

您需要另一个变量来中断循环:

let loop = true;
while (loop){
    const filter = m => m.author.id === message.author.id;
    let cword = rword();

    function shuffle() {
        var a = cword.split("");
        var n = a.length;
        for (var i = n - 1; i > 0; i--) {
            var j = Math.floor(Math.random() * (i + 1));
            var tmp = a[i];
            a[i] = a[j];
            a[j] = tmp;
        };
        return a.join("");
    };

    let aembed = new Discord.MessageEmbed()
        .setTitle("New Word")
        .setDescription(shuffle())
        .setColor("BLUE")
        .setFooter("Word expires in 5 minutes", "https://i.imgur.com/s7R7YC3.png")
        .setTimestamp();

    let rightembed = new Discord.MessageEmbed()
        .setTitle("Correct")
        .setDescription("You got 15 crumb! Please run `.word` to get a new word!")
        .setColor("GREEN")
        .setFooter("Rubber Duck Games", "https://i.imgur.com/s7R7YC3.png")
        .setTimestamp();

    let wrongembed = new Discord.MessageEmbed()
        .setTitle("Wrong")
        .setDescription(`The correct word was **${cword.toLowerCase()}**. Please run \`.word\` to get a new word!`)
        .setColor("RED")
        .setFooter("Rubber Duck Games", "https://i.imgur.com/s7R7YC3.png")
        .setTimestamp();

    message.channel.send(message.author, {
        embed: aembed
    }).then(p1 => {
        let Word = new Discord.MessageCollector(p1.channel, filter, {
            max: 1,
            time: 300000
        });

        Word.on("collect", async cmsg => {
            let WordMSG = cmsg.content.toLowerCase();

            if (WordMSG == cword.toLowerCase()) {
                Word.stop();
                message.channel.send(rightembed);
                db.add(`${message.author.id}.crumb`, 15);
            } else {
                Word.stop();
                message.channel.send(wrongembed);
                loop = false; // stop the loop
            };
        });
    });
};

嗯。。。中断位于另一个函数中。您的
中断
出现在函数回调中,它与您的
while
循环不在同一上下文中执行-这是预期的行为。啊,我不知道这是如何工作的。有没有其他方法可以在这种情况下中断while循环?while中的
while
循环是什么?它是否可以位于
异步函数
内?是的,它位于异步函数内。这是一个无限循环
message.channel.send
是异步的,不会被等待,因此
while
循环将不允许异步任务的事件循环的下一个勾号运行这不是问题所在,是吗?问题是关于中断,代码结构由海报修复。