【发布时间】:2017-09-28 03:07:24
【问题描述】:
我在这里有这个按间隔运行的函数,它的作用是,当它运行时,它设置了 2 个 settimeout。
问题来了,应该在第二次超时之后调用的异步函数在第二次超时甚至触发之前被调用。这是我的代码。
var runner = Interval.run(function() {
//-212965881
bot.sendMessage('-212965881', "Drop usernames now").then(function(){
ready = 1;
list = {};
console.log(ready);
setTimeout(function(){
return bot.sendMessage('-212965881',"Round has begun. Please start liking now. Leech check will begin in 1 minute");
}, 10000);
}).then(function(){
ready = 0;
setTimeout(function(){
return bot.sendMessage('-212965881',"Leech check has begun!");
}, 15000);
}).then(function(){
//This one fires before 15 seconds
let msg = {chat:{}};
msg.chat.id = '-212965881';
return bot.event('/check', msg);
}).catch(function(err){
console.log(err);
});
}, 20000);
不知道为什么会这样。也许我走错了路。 任何人都可以对此有所了解吗?谢谢
【问题讨论】:
-
setTimeout()安排稍后异步运行,它不会暂停执行。您传递给setTimeout()的任何函数的返回值也会被忽略。
标签: javascript node.js asynchronous settimeout