-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwebworker.html
57 lines (43 loc) · 1.14 KB
/
webworker.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Web Worker</title>
<link rel="stylesheet" href="">
</head>
<body>
<h3>需要起一个服务器,同源!</h3>
<p>Count numbers: <b id="result">0</b></p>
<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button>
<script>
let worker = null;
function startWorker () {
if (worker) {
console.log("Web Worker正在运行...", worker);
return worker;
} else {
console.log(worker);
worker = new Worker("./webworker.js");
}
if (typeof(Worker) === "undefinde") {
document.querySelector("#result").innerHTML = "你的浏览器不支持Web Worker...";
} else {
// worker = new Worker("./webworker.js");
console.log("开始计数");
worker.postMessage("start");
worker.onmessage = function(event) {
console.log(event);
document.querySelector("#result").innerHTML = event.data;
}
}
}
function stopWorker () {
console.log("停止计数", worker);
worker.terminate();
worker = null;
}
</script>
</body>
</html>