title: Using setTimout() correctly in Nodejs with a chat bot
body: <p>I'm writing an IRC chat bot that answers to commands. My problem is having a cooldown on commands. </p>
  
  <p>My idea is this:</p>
  
  <pre><code>setTimeout(function() {
    client.on('chat', function (channel,user, message, self) {
      if (message === 'command') {
        // do stuff
      }
    });
  }, 5000);
  </code></pre>
  
  <p>The bot reacts everytime a user types in to chat. then it checks if the user typed the command and then it obviously does stuff.</p>
  
  <p>But my Idea here is not working. The bot keeps answering to the command. I just want him to not answer when he already spoke 5 seconds ago.</p>
  
  <p>I tried it with a timer and date.now and stuff but I think if I define a variable like</p>
  
  <pre><code>var timer = 100000000000000
  </code></pre>
  
  <p>The variable will have that value all the time and I can't change once because it will be re-defined.</p>
  
  <p><a href="http://www.tmijs.org/docs/" rel="nofollow">http://www.tmijs.org/docs/</a> Is the library I am using for my bot</p>
  
  <p>Edit:</p>
  
  <pre><code>function doStuff(channel, user, message, self) {
        // do stuff
  };
  
  
  client.on('chat', function(channel, user, message, self) {
    if (message === 'command') {
      setTimeout( function() {
        client.on('chat', doStuff(channel, user, message,self));
      }, 5000);
      doStuff(channel, user, message, self);
    }
  });
  </code></pre>