JavaScript Web Worker - close() vs terminate()

John Lee picture John Lee · May 28, 2015 · Viewed 15.2k times · Source

I'm not totally understanding the difference between the close() vs terminate() methods for Web Workers. I've read the descriptions here and it seems to be doing the same thing?? http://www.w3.org/TR/workers/

When would I use one over the other?

Answer

Paweł Chorążyk picture Paweł Chorążyk · May 28, 2015

The close() method is visible inside the worker's scope. The terminate() method is a part of the worker object's interface and can be called "from the outside".

If you create a worker in your main script and want to stop it from that script you should call the terminate() on the worker object. If you want to stop the worker from the worker code (for example as a response to an external message) you should call the close() method.

Source: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#Terminating_a_worker