In my code, the message sends every few seconds. How do I make it advance through messages instead of sending the same message over and over?
The first message is sent successfully, the second message needs to be sent with a few seconds delay from the first one.
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
I feel, setInterval should be used for executing a function repeatedly after a defined period instead of setTimeout.
let messages = ["Message 1", "Message 2", "Message 3", "Message 4", "Message 5", "Message 6"];
let counter = 0;
let timer;
function myLoop() {
var date = new Date();
if (!messages[counter]) return clearInterval(timer);
console.log(date + " : " + messages[counter]);
counter++;
}
timer = setInterval(myLoop, 3000);