Javascript 如何发送两条消息(带延迟的第二条消息,punchline)
我试图在js中创建一个笑话,在延迟后发送一条笑话 这是我正在使用的代码。它只给我发了一个自动取款机。 我删掉了一些代码,因为它与这个问题有关Javascript 如何发送两条消息(带延迟的第二条消息,punchline),javascript,bots,delay,Javascript,Bots,Delay,我试图在js中创建一个笑话,在延迟后发送一条笑话 这是我正在使用的代码。它只给我发了一个自动取款机。 我删掉了一些代码,因为它与这个问题有关 var delayInMilliseconds = 1500; function poolJoke() { const rand = Math.floor(Math.random() * 7) + 1; if (rand === 1) { bot.postMessageToChannel( 'bots', 'Wha
var delayInMilliseconds = 1500;
function poolJoke() {
const rand = Math.floor(Math.random() * 7) + 1;
if (rand === 1) {
bot.postMessageToChannel(
'bots',
'What does bread loaves say when they greet each other?',
setTimeout(function() {
bot.postMessageToChannel('bots',
'Gluten tag.')
}, delayInMilliseconds),
params
);
}
}
您好,您需要添加一个局部变量和清理时间,请尝试以下代码
function poolJoke(val) {
var timer;
const rand = val;
if (rand == 1) {
window.clearTimeout(timer);
console.log('What does bread loaves say when they greet each other?');
timer = setTimeout(function () {
console.log('bots', 'Gluten tag.' + timer)
}, 5000);
}
}
尝试我测试过的此解决方案,效果良好:
bot.on("start", function() {
bot.postMessageToChannel(channel, "Hello world!");
console.log("Hello world!");
setTimeout(() => {
bot.postMessageToChannel(channel,'after 5000 ms.')
}, 5000)
});
这适用于控制台,但我似乎无法实现它来与bot一起工作