title: How to stop a function during its execution - JavaScript
body: <p>How can I stop/terminate a function which is already executed and still running? For example, I have this function: </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function foo() {
    setInterval(function() {
      console.log("Foo Executed !");
    }, 3000);
  }
  foo();</code></pre>
  </div>
  </div>
  </p>
  
  <p>Now, this <code>foo()</code> function will run for unlimited time, when a specific event occurs, let's say a stop button has been clicked, then I want to stop this function. </p>
  
  <p>In my case the function doesn't have <code>setInterval()</code> function. And what if the <code>foo()</code> function don't have the <code>setInterval()</code> method, but just many lines of code that get executed and I want to stop it from executing after a specific event. </p>