-
Notifications
You must be signed in to change notification settings - Fork 31
/
workers.html
43 lines (36 loc) · 1.04 KB
/
workers.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
<!doctype html>
<html lang="is">
<head>
<meta charset="utf-8">
<title>workers</title>
</head>
<body>
<button class="sans">Reikna fibonacci runu án worker</button>
<button class="worker">Reikna fibonacci runu með worker</button>
<p>Niðurstaða:</p>
<p class="result"></p>
<script>
function fibo(n) {
if (n < 2) return 1;
return fibo(n - 2) + fibo(n - 1);
}
const sansButton = document.querySelector('button.sans');
const workerButton = document.querySelector('button.worker');
const p = document.querySelector('.result');
sansButton.addEventListener('click', () => {
const results = [];
for (let i = 0; i < 40; i++) {
results.push(fibo(i));
}
p.textContent = results.join(', ');
});
const worker = new Worker('fibo.js');
workerButton.addEventListener('click', () => {
worker.postMessage(40);
worker.onmessage = (event) => {
p.textContent = `Frá worker: ${event.data}`;
};
});
</script>
</body>
</html>