Thunderwolf Thunderwolf - 4 months ago 26
Node.js Question

node.js - setInterval & setTimeout incrementally repeating command

I'm trying to help a friend setup a bot for

Picarto.tv
, we have the bot LINK and there's no default
plugin
there for repeating messages, so I tried to make a very crude (seriously, it's terrible. I'm not a developer.)
plugin
for it, and I tried to use
SetInterval/SetTimeout
, and both times when I use them it will put the message in chat once, at the set interval, then it will wait, then after the interval it will say the message twice, then three times and so on.

It looks like this:

Time 1:
Testing...

Time 2:
Testing...
Testing...


And so on. Here's the code, as I said, it's terribly made, don't bash me too hard for it.

var api;
function handleChatMsg(data) {
var recursive = function () {
api.Messages.send("Testing Bot Repeat...");
setTimeout(recursive,15000);
}
recursive();
}



module.exports = {
meta_inf: {
name: "Repeat Message",
version: "1.0.0",
description: "Repeats a message every 5 minutes. Message and interval can be changed.",
author: "ZX6R"
},
load: function (_api) {
api = _api;
},
start: function () {
api.Events.on("userMsg", handleChatMsg);
}
}


Can anybody help me figure out why it's incrementally saying the message more times?

Answer

Let's not complicate things and make it simpler.

setInterval(function(){xyz()}, 300000); 

var xyz = function()
{
    //Do what you want your function to do.
}

Here function xyz will be called every 300,000 milliseconds which means 5 minutes.

Take a look at Node Cron btw if you are going to use it regularly for an application.

Comments