HTML and CSS Reference
In-Depth Information
The first call to importScripts() imports the Helper1.js file. The second call imports three files:
Helper1.js , Helper2.js , and Helper3.js . The scripts in the second call are executed in the order specified.
Handling Errors
Typically, web workers involve lengthy processing, and at times they may encounter unexpected errors.
Because web workers don't have access to DOM, they can't report the errors by displaying dialog boxes or
alerts. Any unhandled errors in the worker thread raise error event. The web page can wire an event
handler to the error event of the web worker object and be notified when there is any error. Listing 10-4
shows how this is done.
Listing 10-4. Handling Errors
$("#btnStart").click(function () {
worker = new Worker("scripts/Processing.js");
worker.addEventListener("message", ReceiveMessageFromWorker, false);
worker.addEventListener(“error”, HandleError, false);
worker.postMessage("Hello Worker!");
function HandleError(evt) {
var msg="There was an error in the worker thread!\r\n";
msg += "Message : " + evt.message + "\r\n";
msg += "Source : " + evt.filename + "\r\n";
msg += "Line No. : " + evt.lineno;
Notice the line of code shown in bold. The addEventListener() method attaches an event-handler
function HandleError() to the error event. The HandleError() function receives an ErrorEvent object. The
three properties of the ErrorEvent message , filename, , and lineno —give you the error message, the file
name in which error occurred, and the line number at which the error occurred. Figure 10-4 shows a
sample run of the HandleError() event-handler function.
Figure 10-4. Error details being displayed by handling the error event
The alert box shows the error message “Uncaught Unexpected Error !!!”. It also tells the user that the
source of the error is Processing.js and that there is problem at line 6.
Search WWH ::

Custom Search