web works importScripts

html:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>web works 日志文件输出</title>
  <script>
    var worker = new Worker("worker-hello.js");
    worker.onmessage = function (e) {
      if (e.data.type == "log") {
        console.log(e.data.msg);
      }
    }

    function sendMsg() {
      var username = document.getElementById("username").value;
      worker.postMessage(username);
    }
  </script>
</head>
<body>
<h2>importScripts使用</h2>
请输入用户名
<input type="text" id="username">
<button onclick="sendMsg()">登陆</button>
</body>
</html>

 worker-hello.js:

importScripts("worker-log.js")
//接收信息
onmessage = function (e) {
  log("Worker收到了信息" + e.data);
}

worker-log.js:

function log(msg) {
  //输出日志
  postMessage({
    type: "log",
    msg: msg
  });
}

   

原文地址:https://www.cnblogs.com/zhishaofei/p/4262862.html