This example demonstrates how to create a simple worker that can be started and stopped with buttons. Note that this code requires a web server to run, due to browser security restrictions.
<!DOCTYPE html> <html> <head> <title>XoaX.net's JavaScript: An Example Worker</title> <script type="text/javascript"> var qWorker = null; function Play() { if (qWorker == null) { qWorker = new Worker("WorkerScript.js"); qWorker.onmessage = function(qEvent) { document.getElementById("idSign").innerHTML = qEvent.data; } } } function Pause() { if (qWorker != null) { qWorker.terminate(); qWorker = null; } } </script> </head> <body> <button onclick="Play()">Play</button> <button onclick="Pause()">Pause</button> <h1 id="idSign">Nothing</h1> </body> </html>
var saSigns = ["Stop", "Go", "Caution", "Yield", "Halt", "Walk"]; var iIndex = 0; function ChangeSign() { iIndex = ((iIndex + 1) % saSigns.length); postMessage(saSigns[iIndex]); setTimeout("ChangeSign()",500); } ChangeSign();
© 20072025 XoaX.net LLC. All rights reserved.