如果超时到期,如何跳过一段代码

问题描述 投票:0回答:1

我使用discord.js编写一个简单的discord bot。我正在制作一个“踢”命令,如果用户在x秒内没有发送“确认”,则取消整个命令。问题是我不知道如何设置超时。如果超时到期,我如何跳过一些代码?

javascript discord.js
1个回答
0
投票

注意:如果确认您的意思是确认();命令你无法取消它运行或运行另一个代码,而确认是活动的,因为confirm();阻止。在这种情况下,我建议检查非阻塞方法。

//Declare a global boolean variable
var expired=false;
function expireIt(){
    expired=true;
}
//x seconds
//three secs for this example
let x=3000;
setTimeout(expireIt(), x);

function whateverYouRun(){
//if expired is true do nothing
if(expired){
//do nothing, time expired
}else{
//your code goes here
}
}
© www.soinside.com 2019 - 2024. All rights reserved.