【发布时间】:2021-10-06 23:37:08
【问题描述】:
在我的代码中,消息每隔几秒发送一次。如何让它通过消息推进而不是一遍又一遍地发送相同的消息?
第一条消息发送成功,第二条消息需要比第一条延迟几秒发送。
const tmi = require('tmi.js');
var value = true;
const options = {
options: {
debug: true,
},
connection: {
cluster: 'aws',
reconnect: true,
},
identity: {
username: 'yessirski69', //username of the bot
password: 'yolopoo', //login token of the bot
},
channels: ['mrpooski'], // the channel you are targetting
};
const client = new tmi.client(options);
client.connect();
var i = 1; // set your counter to 1
function myLoop() { // create a loop function
setTimeout(function() { // call a 3s setTimeout when the loop is called
client.action('mrpooski', 'Hello This is the first message'); // your code here
i++; // increment the counter
if (i < 1000) { // if the counter < 10, call the loop function
myLoop(); // .. again which will trigger another
} // .. setTimeout()
}, 3000)
}
myLoop(); // start the loop
【问题讨论】:
-
简单的设置超时应该可以工作
-
嘿@Bharat,您知道如何将其写入代码吗?
-
消息是否存储在数组中?
-
@denzaa 欢迎来到 SO!当前代码到底有什么问题?
-
第二条消息在哪里?消息是如何存储的?例如,您可以在每次循环运行时推进一组消息。
标签: javascript node.js iteration settimeout