<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Start/Stop Web Worker in HTML5</title> <script> // Set up global variable var worker; function startWorker() { // Initialize web worker worker = new Worker("examples/resources/worker.js"); // Update result, when we get a message from worker worker.onmessage = function (event) { document.getElementById("result").innerHTML = event.data; }; // Tell worker to get started worker.postMessage("start"); } function update(event) { // Update the page with current message from worker document.getElementById("result").innerHTML = event.data; } function stopWorker() { // Stop the worker worker.terminate(); } </script> </head> <body> <h1>Web Worker Demo page</h1> <button onclick="startWorker();" type="button">Begin web worker</button> <button type="button" onclick="stopWorker();">End web worker</button> <div id="result"> <!--Received messages will be inserted here--> </div> </body> </html>