forked from greenido/Web-Workers-hacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
highPrime2.html
83 lines (79 loc) · 1.75 KB
/
highPrime2.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
<!DOCTYPE HTML>
<html>
<head>
<title>Web Worker: The highest prime number</title>
<script src="http://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: lightyellow;
margin:8px;
}
article {
position: relative;
color:red;
}
input {
width: 80px;
height: 35px;
font-size: 120%;
}
</style>
<body>
<script>
var myWorker;
function start() {
console.log("WebWorker: Starting");
myWorker = new Worker("highPrime2.js");
myWorker.addEventListener("message", primeHandler, false);
var maxNum = $('#upto').val();
myWorker.postMessage({'cmd': 'start', 'upto': maxNum});
}
function stop() {
if (myWorker != undefined) {
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 {
$('#status').append(JSON.stringify(event.data) );
}
}
function is_numeric(input){
return typeof(input)=='number';
}
</script>
<h1>Web Worker: The highest prime number</h1>
<article>The prime numbers:
<output id="result"></output>
<div id="status"></div>
</article>
<div id="actions">
<input type="text" name="upto" id='upto' value="42"/>
<button onclick="start()" title="Start the work">Start</button>
<button onclick="stop()" title="Stop the work and go have a drink">Stop</button>
</div>
</body>
</html>