title: How to get clearInterval() to work in a loop in JavaScript
body: <p>So I'm trying to get a function to run once every second, and then after four seconds I want it to stop using <code>clearInterval()</code></p>
  
  <pre><code>function dotdotdot(){
  var x = 0;
  setInterval(function(){
      if (x&gt;=3){
      torpWri = torpWri + ".";
      document.getElementById("torpTxt").innerHTML = torpWri;
      x++;
      }
      else{
          x = 0;
          clearInterval();
      }
      },1000);
  }
  </code></pre>
  
  <p>This is my function and it should stop after four seconds and then reset x to 0 for when I call it again.</p>
  
  <pre><code>function loadButton(){
  torpWri = "Torpedo Loading"
  if(torpLoadAmount[arNum]&lt;5){
      torpLoadAmount[arNum]++;
      torpAmount--;
      document.getElementById("torpCnt").innerHTML = torpAmount;
      document.getElementById("torpTxt").style.visibility = "visible";
      document.getElementById("butunload").disabled=true;
      document.getElementById("butfire").disabled=true;
      document.getElementById("torpTxt").innerHTML = torpWri;
      dotdotdot();
  }
  else{
      document.getElementById("torpTxt").style.visibility = "visible";
      document.getElementById("torpTxt").innerHTML = "Torpedo Bay Full";
  }
  timer3();
  }
  </code></pre>
  
  <p>This is how I'm calling it.
  I'm just needed to know why it isn't running the function <code>dotdotdot();</code> every second and then stopping after four. Then when I call it again it should all just reset. But it's not running...</p>
  
  <p>I've been searching for a while and haven't found anything, so I came here.</p>
  
  <p>(Also, please don't comment on my other code, I know there are probably easier ways to do it, but this is what I'm working with right now.)</p>