如何让我的代码在执行其余功能之前先等待一个功能?

我正在为不和谐的机器人做某事,但我无法弄清楚这部分...

discord一个聊天应用程序,您可以在其中创建服务器和供人们交谈的渠道,还有机器人,这就是我在做的事情。

基本思想是我编写了一个命令,并且该机器人拒绝了每个人都可以在聊天中交谈的权限,倒计时开始,并且当倒计时达到0时,该机器人允许再次交谈,是的,这是一个JoJo参考。我已经准备好所有允许和拒绝的内容,只需要进行此倒计时即可。

我的问题是我不能让代码等待自己。

//some other code

    console.log("Toki uo tomare!")

    var x = 0;
    function  go() {
        console.log(x)
        if (x++ < 5) {
            setTimeout(go,500);
        }
    }
    go();

    console.log("Toki wa ugokidasu...")

//some other code

我希望看到

Toki uo tomare! 0 1 2 3 4 5 Toki wa ugokidasu...

但是我看到了这个

Toki uo tomare! 0 Toki wa ugokidasu... 1 2 3 4 5

salalaf 回答:如何让我的代码在执行其余功能之前先等待一个功能?

const TalkingDuration=5000;
let canTalk=true;

window.botInterval=setTimeout(function(){
  canTalk=false;
},TalkingDuration);

setInterval(function(){
  if(canTalk) console.log("Toki uo tomare!");
},100);

//So the clients can talk for 5s and then the bot will stop the chat you can use a loop to redo the task each 5s just by adding another setInterval ... Hope you like it

本文链接:https://www.f2er.com/3131086.html

大家都在问