title: JS: check if setTimeout has finished or been canceled
body: <p>I am creating a script that will execute a task after a certain amount of time has passed. I want the user to be able to cancel execution of these tasks. I can do this by saving the timeout to a list which I then use to cancel all timeouts. However if the timeout completes normally it is still stored in that list.</p>
  
  <p>How do I check if a timeout has already been completed or canceled before attempting to clear it?</p>
  
  <pre><code>var timeouts = [];
  
  $(document).on('click', '.doTask', function() {
      var timeout = setTimeout(() =&gt; {
          doTaskAfter();
      }, 10000);
      timeouts.push(timeout);
  });
  
  $(document).on('click', '.cancelTasks', function() {
      var i = timeouts.length;
      while (i--) {
          if (timeouts[i].finished || timeouts[i].timeoutCleared) { // How to I check this?
              clearTimeout(timeouts[i]);
          }
          timeouts.splice(i, 1);
      }
  });
  </code></pre>