-
Notifications
You must be signed in to change notification settings - Fork 23
/
high-prime.html
89 lines (84 loc) · 2.01 KB
/
high-prime.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!DOCTYPE HTML>
<html>
<head>
<title>Web Worker: The highest prime number</title>
<!-- Get the latest jQuery code -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
</head>
<style>
#actions {
position: fixed;
top: 10px;
background: lightBlue;
padding:8px;
}
h1 {
position: relative;
bottom: 10px;
left: 280px;
}
#status {
position: relative;
font-size: 120%;
background: darkslategrey;
padding: 20px;
border-radius: 20px;
}
article {
position: relative;
color:yellow;
background: darkgray;
padding: 25px;
}
input {
width: 80px;
height: 35px;
font-size: 120%;
}
</style>
<body>
<h1>Web Worker: The highest prime number</h1>
<article>
<h2>The prime numbers</h2>
<output id="result"></output>
<div id="status"></div>
</article>
<div id="actions">
<input type="text" name="upto" id="upto"/>
<button onclick="start()" title="Start the work">Start</button>
<button onclick="stop()" title="Stop the work and go have a drink">Stop</button>
</div>
<script>
var myWorker;
function start() {
console.log("WebWorker: Starting");
myWorker = new Worker("high-prime.js");
myWorker.addEventListener("message", primeHandler, false);
var maxNum = $("#upto").val();
myWorker.postMessage({ cmd: "start", upto: maxNum });
}
function stop() {
if (myWorker) {
var msg = "<br/>WebWorker: Terminating " + new Date();
console.log(msg);
$("#status").append(msg);
myWorker.terminate();
myWorker = null;
}
}
function primeHandler(event) {
console.log("got e:" + event.data);
if (is_numeric(event.data)) {
$("#result").append(event.data);
} else {
// update our status div and remove the " so the text will be clear
$("#status").append(JSON.stringify(event.data).replace(/"/g, ""));
}
}
function is_numeric(input) {
return typeof input == "number";
}
</script>
</body>
</html>