JavaScript的 Web Worker 示例代码

完整的 Web Worker 实例代码
我们已经在 .js 文件中看到了 Worker 代码。下面是 HTML 页面的代码:
 
实例
<!DOCTYPE html>
<html>
<body>
 
<p>Count numbers: <output id=”result”></output></p>
<button onclick=”startWorker()”>Start Worker</button>
<button onclick=”stopWorker()”>Stop Worker</button>
 
<script>
let w;
 
function startWorker() {
  if (typeof(w) == “undefined”) {
    w = new Worker(“demo_workers.js”);
  }
  w.onmessage = function(event) {
    document.getElementById(“result”).innerHTML = event.data;
  };
}
 
function stopWorker() {
  w.terminate();
  w = undefined;
}
</script>
 
</body>
</html>

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h64499.shtml

张贴在3