Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Web Worker Two Way Communication -HTML
<!DOCTYPE html>
<html xmlns="">
<button onclick="sayHello()">Say HI</button>
<button onclick="unknownCmd()">Send unknown command</button>
<button onclick="stop()">Stop worker</button>
<output id="workermessage"></output>
function sayHello() {
worker.postMessage({ 'cmd': 'start', 'msg': 'Hello' });
function stop() {
// Calling worker.terminate() from this script would also stop the worker.
worker.postMessage({ 'cmd': 'stop', 'msg': 'Bye' });
function unknownCmd() {
worker.postMessage({ 'cmd': 'notsupported', 'msg': '???' });
var worker = new Worker('3-TwoWay.js');
worker.addEventListener('message', function (e) {
document.getElementById('workermessage').textContent =;
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment