-
Notifications
You must be signed in to change notification settings - Fork 2
/
web-worker.html
56 lines (45 loc) · 1.33 KB
/
web-worker.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<h4>Fibonacci Series:</h4>
<h6>Enter value of n: <input type="text" name="" value="0" id="input"></h6>
Result: <h5 id='result'></h5>
<button type="button" name="button" onclick="startWorker()">Submit</button>
<button type="button" name="button" onclick="stopWorker()">Terminate Woker</button>
<script type="text/javascript">
var myWorker = undefined;
if(window.Worker){
console.log('Browser Support Web Worker.');
console.log('Going to start Worker.');
myWorker = new Worker('web-worker.js');
myWorker.onerror = function (e) {
console.log("Error Occured.");
}
}
function startWorker() {
var n = document.getElementById('input').value;
myWorker.postMessage(n)
myWorker.onmessage = function (e) {
console.log("Main I/O Context:");
console.log('Message Received from Web Worker.');
wait(8000)
document.getElementById('result').innerHTML = e.data;
}
}
function stopWorker() {
console.log('Web Worker terminated.');
myWorker.terminate()
}
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
</script>
</body>
</html>