title: Javascript stop interval and action after
body: <p>My proble is about setintervall function. I call a function named <code>thrice()</code> when <code>first()</code> and <code>second()</code> execution is finished. There is no proble with that. The code below :</p>
  
  <pre><code>var oneFinish = false;
  var twoFinish = false;
  
  function first() {
      console.log("FUNCTION first RUNNING");
      for (i = 0; i &lt; 5; i++) {
          console.log("first " + i);
      }
      console.log("FUNCTION first FINISH");
      oneFinish = true;
  }
  
  function second() {
      console.log("FUNCTION second RUNNING");
      for (i = 0; i &lt; 10; i++) {
          console.log("second " + i);
      }
      console.log("FUNCTION second FINISH");
      twoFinish = true;
  }
  
  function thrice() {
      var intev = setInterval(function () {
          if (oneFinish &amp;&amp; twoFinish) {
              console.log("FUNCTION thrice RUNNING");
              oneFinish = false;
              twoFinish = false;
              clearInterval(intev);
          }
      }, 3000);
      console.log("FUNCTION thrice FINISH");
  }
  
  first();
  second();
  thrice();
  </code></pre>
  
  <p>The output is like this :</p>
  
  <pre><code>FUNCTION first RUNNING
  first 0
  first 1
  first 2
  first 3
  first 4
  FUNCTION first FINISH
  FUNCTION second RUNNING
  second 0
  second 1
  second 2
  second 3
  second 4
  second 5
  second 6
  second 7
  second 8
  second 9
  FUNCTION second FINISH
  FUNCTION thrice FINISH
  FUNCTION thrice RUNNING
  </code></pre>
  
  <p>You see at the <strong>end</strong> of the output, it's the problem <code>FUNCTION thrice FINISH</code> is executed before <code>FUNCTION thrice RUNNING</code></p>