HTML and CSS Reference
In-Depth Information
12.
Enter the code shown in Listing 5-3 for its implementation. i will explain this in more
detail later in the chapter.
Listing 5-3. The controlWorker.js implementation
/* This file contains functions used to
communicate with the web worker */
var myWorker;
function createWorker() {
if (typeof(Worker) !== "undefined") {
var log=document.querySelector("#output");
log.value +="Starting worker process... ";
myWorker=new Worker("Scripts/worker.js");
log.value +="Adding listener... ";
myWorker.onmessage=function(event){
log.value +=event.data + "\n";
}
log.value +="Done!\n";
}
else {
alert("Your browser does not support web workers");
}
}
function sendWorkerMessage(){
if (myWorker !== null) {
var log=document.querySelector("#output");
log.value +="Sending message... ";
var message=document.querySelector("#message");
myWorker.postMessage(message.value);
log.value +="Done!\n";
}
}
function closeWorker(){
if (myWorker !== null) {
var log=document.querySelector("#output");
log.value +="Closing worker... ";
myWorker.terminate;
myWorker=null;
log.value +="Done!\n";
}
}
13.
Press F5 to debug the application. Click the Create Worker button and then click the
Send button. modify the message and try clicking the Send message button again.
Finally, click the Close Worker button. The text area should look like Figure 5-7 .
 
Search WWH ::




Custom Search