How to handle errors in HTML5 Web Workers?

JavascriptWeb DevelopmentFront End Scripts

The following shows an example of an error handling function in a Web Worker JavaScript file that logs errors to the console.

Example

<!DOCTYPE HTML>
<html>
   <head>
      <title>Big for loop</title>
      <script>
         var worker = new Worker('bigLoop.js');
         worker.onmessage = function (event) {
            alert("Completed " + event.data + "iterations" );
         };
         worker.onerror = function (event) {
            console.log(event.message, event);
         };
         function sayHello(){
            alert("Hello sir...." );
         }
      </script>
   </head>
   <body>
      <input type = "button" onclick = "sayHello();" value = "Say Hello"/>
   </body>
</html>
raja
Published on 06-Apr-2018 17:10:55
Advertisements