Skip to content

Instantly share code, notes, and snippets.

@anthonyalvarez
Last active November 2, 2018 19:39
Show Gist options
  • Select an option

  • Save anthonyalvarez/d867390a20134e42a417b37f0067b1df to your computer and use it in GitHub Desktop.

Select an option

Save anthonyalvarez/d867390a20134e42a417b37f0067b1df to your computer and use it in GitHub Desktop.
Web Workers Introduction

Web Workers Introduction

Web Workers are a way that JavaScript has to start running tasks on a new thread. This helps to offload some heavy processing for your web pages.

There are really only two things you need for web workers.

  worker.postMessage( data ) 
  worker.addEventListener('message', func) 

If you need to shut down a worker you can call the self.close( ) method from within the worker or the worker.terminate( ) method from the initiating script.

To send commands to and from service worker involves

  postMessage(string || object);
  addEventListener('message', doSomething());

Arguments of postMessage can be strings, objects and etc. Function that runs when web worker gets message.

The object's message is always in .data property of object. The method postMessage() sends message back to calling script.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment