title: Is it possible to terminate a running web worker?
body: <p>I have a web worker running a time-consuming routine task with ajax-requests. Can I terminate them from a main thread not waiting for them to finish?</p>
  
  <p>That's how I spawn and terminate it:</p>
  
  <pre><code>$("button.parse-categories").click(function() {
      if (parseCategoriesActive==false) {
          parseCategoriesActive = true;
          parseCategoriesWorker = new Worker("parseCategories.js");
  
  
          $("button.parse-categories-cancel").click(function() {
              parseCategoriesWorker.terminate();
              parseCategoriesActive = false;
          });             
      }
  });
  </code></pre>
  
  <p>This is the worker code:</p>
  
  <pre><code>function myAjax(url, async, callback) {
      xmlhttp = new XMLHttpRequest();
      xmlhttp.onreadystatechange=function() {
          if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)
              callback(xmlhttp.responseText);
          if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status!=200) {
              self.postMessage("error");
              throw "error in ajax: "+xmlhttp.status;
          }
      }
      xmlhttp.open("GET", url, async);
      xmlhttp.send();
  }
  
  var parseCategoriesActive = true;
  var counter = 0;
  do {
      myAjax('parser.php', false, function(resp) {
          if (resp=='success')
              parseCategoriesActive = false;
          else {
              counter += Number(resp);
              self.postMessage(counter);
          }       
      });
  } while (parseCategoriesActive==true);
  </code></pre>