-
Notifications
You must be signed in to change notification settings - Fork 18
/
Example-pi.html
98 lines (90 loc) · 2.24 KB
/
Example-pi.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
90
91
92
93
94
95
96
97
<!DOCTYPE HTML>
<html>
<head>
<title>Web Worker: The Run After Pi</title>
<!-- Get the latest jQuery code -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
</head>
<style>
#actions {
position: relative;
top: 10px;
background: cadetblue;
padding:8px;
border-radius:8px;
width: 445px;
}
#status {
position: relative;
font-size: 120%;
background: darkblue;
padding: 20px;
border-radius: 20px;
}
article {
position: relative;
color:yellow;
background: darkgray;
padding: 5px;
width: 450px;
border-radius:8px;
}
input {
width: 150px;
height: 35px;
font-size: 120%;
}
button {
height: 35px;
position: relative;
bottom: 3px;
}
</style>
<body>
<h1>Web Worker: The Run After Π</h1>
<article>
<h2>The Calaulated Π</h2>
<output id="result"></output>
<div id="status"></div>
</article>
<div id="actions">
<label>Cycles</label>
<input type="text" name="upto" id="upto"/>
<button onclick="start()" title="Start the run">Start</button>
<button onclick="stop()" title="Stop the run">Stop</button>
</div>
<script>
var worker;
function start() {
console.log("WebWorker: Starting");
worker = new Worker("Example-pi.js");
worker.addEventListener("message", primeHandler, false);
var maxNum = $('#upto').val();
worker.postMessage({'cmd': 'start', 'upto': maxNum});
}
function stop() {
if (worker) {
var msg = "<br/>WebWorker: Terminating " + new Date();
console.log(msg);
$('#status').append(msg);
worker.terminate();
worker = 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>