https://developer.mozilla.org/zh-CN/docs/Web/API/Worker
就是单独去执行一个任务,不影响主线程的任何操作
案例:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>worker</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>工作: <output id="result"></output></p>
<button onclick="startWorker()">开始工作</button>
<button onclick="stopWorker()">停止工作</button>
<hr />
<input type="text">
<script>
var W;
function startWorker() {
W = new Worker("worker.js");
console.log(W);
W.onmessage = function (event) {
console.log(event);
document.getElementById("result").innerHTML = event.data;
};
}
function stopWorker() {
W.terminate();
W = undefined;
}
</script>
</body>
</html>
var i = 0;
setInterval(() => {
i = i + 1;
postMessage(i);
}, 1000);
效果:
|