Reputation: 131
My web worker calls close() repeatedly but doesn't stop. Here is the worker's script:
self.onmessage = function (e) {
close();
for (c in e.data) {
postMessage(e.data[c]);
close();
}
}
The for loop executes without issue. Can someone explain why?
Upvotes: 1
Views: 491
Reputation: 6473
It will simply close the worker from that point forward, but the current onmessage event will continue to be handled by this function i.e. the for loop will be executed. You would need to employ some logic in the worker that would update a boolean value and you would then check that boolean prior to executing the for loop.
If you don't want the for loop to execute, then you'd need to do something like this instead:
self.onmessage = function(e) {
const valid = <SOME BOOLEAN LOGIC HERE>;
if (valid) {
for (c in e.data) {
postMessage(e.data[c]);
}
} else {
close();
}
}
Upvotes: 2