Thunderwolf Thunderwolf - 1 year ago 64
Node.js Question

node.js - setInterval & setTimeout incrementally repeating command

I'm trying to help a friend setup a bot for
, we have the bot LINK and there's no default
there for repeating messages, so I tried to make a very crude (seriously, it's terrible. I'm not a developer.)
for it, and I tried to use
, 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:

Time 2:

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...");

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 Source

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.