Javascript 代码问题-消息立即发送,而不是等待超时
代码问题-消息立即发送,而不是等待超时发生 正如代码中所示,它应该需要消息中提到的时间。 我不明白发生了什么事情,这根本不允许它工作Javascript 代码问题-消息立即发送,而不是等待超时,javascript,node.js,discord,discord.js,Javascript,Node.js,Discord,Discord.js,代码问题-消息立即发送,而不是等待超时发生 正如代码中所示,它应该需要消息中提到的时间。 我不明白发生了什么事情,这根本不允许它工作 client.on(“消息”,异步(消息)=>{ 定时器=假; 如果(message.content.startsWith(“timer”))timer=true; 如果(计时器===真){ timewait=message.content.slice(6); integertime=parseInt(timewait); mili=整数时间*60000; set
client.on(“消息”,异步(消息)=>{
定时器=假;
如果(message.content.startsWith(“timer”))timer=true;
如果(计时器===真){
timewait=message.content.slice(6);
integertime=parseInt(timewait);
mili=整数时间*60000;
setTimeout(message.channel.send(“messgae”),mili);
}
});
setTimeout需要传递一个函数<代码>设置超时(函数,毫秒)
尝试执行
setTimeout(函数(){message.channel.send(“messgae”);},mili)代码>问题是您正在调用setTimeout
中的函数,因此它将立即运行。您可以创建一个新函数,该函数返回如下函数:
client.on("message", async (message) => {
if (message.content.startsWith("timer")){
let MessageArray = message.content.slice().trim().split(" ") // Creats an Array by seperating the Message into the Array
let integertime = parseInt(MessageArray[1]); // Taking the second Input of your Message and parsing it into an Integer
let mili = integertime * 60000
setTimeout(function(){
message.channel.send("message")
}, mili) // Sends the Message by using the second Input you've given in your command multiplied by your value in mili as your value for the Timer.
}
})
setTimeout(()=>message.channel.send(“message”),mili);
通过这种方式,您可以传递一个函数而不是一个值。也许您正在寻找类似以下内容:
client.on("message", async (message) => {
if (message.content.startsWith("timer")){
let MessageArray = message.content.slice().trim().split(" ") // Creats an Array by seperating the Message into the Array
let integertime = parseInt(MessageArray[1]); // Taking the second Input of your Message and parsing it into an Integer
let mili = integertime * 60000
setTimeout(function(){
message.channel.send("message")
}, mili) // Sends the Message by using the second Input you've given in your command multiplied by your value in mili as your value for the Timer.
}
})
请参阅MDN文档中的