I use web worker in my project: one simple question is when ever we terminate the web worker event auto unbind from worker or not:
worker = new Worker("scripts/workers/clockTime-worker.js");
worker.addEventListener("message", onMessage);
function onMessage(evt) {
//------
}
worker.terminate();
is message event auto unbind from worker or not?
No, the message event is still bound to the worker object.
You can remove the event using the removeEventListener() property of the worker object or, since you're probably done with the worker anyway, set the worker object to null to ensure it is garbage collected.
Here is a test you can use to ensure this on your platform:
var worker = new Worker( URL.createObjectURL( new Blob([''])));
var e = new MessageEvent('message');
function test() { console.log('Event fired') }
worker.addEventListener('message', test);
worker.dispatchEvent(e); //Event fired
worker.terminate();
worker.dispatchEvent(e); //Event fired
I'm getting the same results in chrome and firefox.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With