SHARE
TWEET

Untitled

a guest Sep 17th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta name="viewport" content="width=device-width">
  6.     <title>Web Workers Example</title>
  7.     <script>
  8.         let myWorker;
  9.         function startWorker(){
  10.             var para = document.createElement("P");
  11.             para.innerText = "Worker Started";
  12.             document.body.appendChild(para);
  13.             if (window.Worker) {
  14.                 myWorker = new Worker('worker.js');
  15.                 myWorker.onmessage = function (e) {
  16.                     var para = document.createElement("P");
  17.                     para.innerText = e.data;
  18.                     document.body.appendChild(para);
  19.                 };
  20.             }
  21.         }
  22.         function terminateWorker(){
  23.             if(myWorker!=undefined){
  24.                 myWorker.terminate();
  25.                 myWorker=undefined;
  26.                 var para = document.createElement("P");
  27.                 para.innerText = "Worker Terminated";
  28.                 document.body.appendChild(para);
  29.             }
  30.         }
  31.         function checkBlocking() {
  32.             var para = document.createElement("P");
  33.             para.innerText = "It works, not blocked by CPU expensive operation";
  34.             document.body.appendChild(para);
  35.         }
  36.     </script>
  37.     <style>
  38.         button {
  39.             display: inline-block;
  40.             padding: 10px 20px;
  41.             background: #2196F3;
  42.             color: white;
  43.             cursor: pointer;
  44.             border: none;
  45.             font-size: 16px;
  46.             margin: 30px;
  47.         }
  48.     </style>
  49. </head>
  50.  
  51. <body>
  52.     <button onclick="startWorker()">Start Worker</button> <br>
  53.     <button onclick="terminateWorker()">Terminate Worker</button><br>
  54.     <button onclick="checkBlocking()">Test browser responsive</button>
  55. </body>
  56.  
  57. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top