title: node.js - setInterval & setTimeout incrementally repeating command
body: <p>I'm trying to help a friend setup a bot for <code>Picarto.tv</code>, we have the bot <a href="https://github.com/Wolvan/PicartoChatBot" rel="nofollow">LINK</a> and there's no default <code>plugin</code> there for repeating messages, so I tried to make a very crude (seriously, it's terrible. I'm not a developer.) <code>plugin</code> for it, and I tried to use <code>SetInterval/SetTimeout</code>, 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.</p>
  
  <p>It looks like this:  </p>
  
  <pre><code>Time 1:  
  Testing...  
  
  Time 2:  
  Testing...  
  Testing...  
  </code></pre>
  
  <p>And so on. Here's the code, as I said, it's terribly made, don't bash me too hard for it.</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>Can anybody help me figure out why it's incrementally saying the message more times?</p>
  
  <p>EDIT: Issue fixed, new code is</p>
  
  <pre><code>var api;
  // Function to call for the repeating
  function handleChatMsg() {
  // This sets the interval of 5 minutes, and calls the variable. Edit the numbers after the comma to change the interval. You MUST put it into milliseconds.
  setInterval(function(){xyz()}, 15000); 
  // This sets the variable, edit the text in "api.Messages.send" to change what the bot repeats.
  var xyz = function()
  {
      api.Messages.send("Testing...")
  }
  }
  
  
  // defines some information about the plugin, and sets up stuff we need.
  module.exports = {
      meta_inf: {
          name: "Repeat Message",
          version: "1.1.1",
          description: "Repeats a message every 5 minutes. Message and interval can be changed.",
          author: "ZX6R"
      },
  load: function (_api) {
        api = _api;
    },
      start: function () {
          handleChatMsg();
      }
  }
  
  // The MIT License (MIT)
  
  // Copyright (c) 2016 RedFalconv2 - ZX6R - WalnutGaming
  
  //Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  
  // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  </code></pre>