0

I want a while loop running forever and when i click on a button it stops. But, while the while loop is running i want to be able to do other things on the webpage. I tried this, but it doesn't work because while the loop is running the worker isn't able to receive any messages.

main.js
---------

let w = new Worker("workers.js");

document.getElementById("stop").onclick = function() {
    w.postMessage("stop");
    w.terminate();
    w = undefined;
}


workers.js
------------

let a = true;

onmessage = function(e) {
    a = false;
}

while(a)
{
    console.log(1);
}
  • 1
    Well, as you have discovered, that won't work. What is it that you *really* want to do in the web worker? – Pointy Jan 22 '21 at 14:51
  • The user can put in a few characters like: "abcdefghjil " in a textbox, the worker then will generate random characters over and over untill the exact same text is found. – Slurkten Jan 22 '21 at 14:54

0 Answers0