title: Prevent JavaScript function from running twice (setTimeout)
body: <p>I have this function that runs for several seconds with the use of <code>setTimeout</code>. This function is ran when a button is clicked.</p>
  
  <pre><code>function complete() {
      var div = document.getElementById('log');
      setTimeout(function(){ div.innerHTML = div.innerHTML + intro[Math.floor(Math.random() * intro.length)] + "&lt;br&gt;"; }, 500);
      setTimeout(function(){ div.innerHTML = div.innerHTML + second[Math.floor(Math.random() * second.length)] + "&lt;br&gt;"; }, 2560);
      setTimeout(function(){ div.innerHTML = div.innerHTML + third[Math.floor(Math.random() * third.length)] + "&lt;br&gt;"; }, 4860);
      setTimeout(function(){ div.innerHTML = div.innerHTML + fourth[Math.floor(Math.random() * fourth.length)] + "&lt;br&gt;"; }, 7860);
      setTimeout(function(){ div.innerHTML = div.innerHTML + fifth[Math.floor(Math.random() * fifth.length)] + "&lt;br&gt;"; }, 9640);
  }
  </code></pre>
  
  <p>However, if the user clicks the button multiple times, this function begins to excute multiple times as well. I have tried to prevent this from occuring by using the code below, however, it is not working.</p>
  
  <pre><code>var running;
  function complete() {
      if (running == true) { alert('error'); }
      running = true;
      var div = document.getElementById('log');
      setTimeout(function(){ div.innerHTML = div.innerHTML + intro[Math.floor(Math.random() * intro.length)] + "&lt;br&gt;"; }, 500);
      setTimeout(function(){ div.innerHTML = div.innerHTML + second[Math.floor(Math.random() * second.length)] + "&lt;br&gt;"; }, 2560);
      setTimeout(function(){ div.innerHTML = div.innerHTML + third[Math.floor(Math.random() * third.length)] + "&lt;br&gt;"; }, 4860);
      setTimeout(function(){ div.innerHTML = div.innerHTML + fourth[Math.floor(Math.random() * fourth.length)] + "&lt;br&gt;"; }, 7860);
      setTimeout(function(){ div.innerHTML = div.innerHTML + fifth[Math.floor(Math.random() * fifth.length)] + "&lt;br&gt;"; }, 9640);
      running = false;
  }
  </code></pre>
  
  <p>What approach should I take, or how can I fix my code so it can accomplish what I am trying to do?</p>