Skip to main content
Inspiring
December 1, 2023
Question

[VIDEO] Terminating a Web Worker terminates "everything"

  • December 1, 2023
  • 0 replies
  • 117 views

Terminating a Web Worder in an extension seem to terminate "everything". I have a simple code to demonstrate.

index.html

 

<!DOCTYPE html>
<html>
<body>
  <button onclick="startWorker()">Start Worker</button>
  <button onclick="terminateWorker()">Terminate Worker</button>
</body>
<script>
  var w;
  function startWorker() {
    if (typeof Worker !== "undefined") {
      if (typeof w == "undefined") {
        w = new Worker("demo_workers.js");
      }
     }
  }

  function terminateWorker() {
    if (w) {
      w.terminate();
      w = undefined;
	  console.log("Worker Terminated");

    }
  }
</script>
</html>

 

demo_workers.js:

 

function hiWorker() {
console.log("hi");
setTimeout("hiWorker()", 1000);
}
hiWorker();

 

One button spawns a worker that's logs "hi" once every second until "Terminate Worker" is pressed. Everything works as expected when run in a web environment, and the console looks something like this:

 

hi
hi
hi
hi
Worker Terminated

 

I get the same results when running this in CEP/extension. but when I click on "Terminate Worker" everything resets, even the console clears and there's a quick little flicker/glitch on the panel. Any ideas?

 

This topic has been closed for replies.