Debian Codegraph @

workerpool/examples/browser/dedicatedWorker.html

raw symbols SCIP
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Worker pool in the browser</title>
5 <style>
6 body {
7 width: 600px;
8 font: 11pt arial;
9 }
10 </style>
11
12 <script src="../../dist/workerpool.js"></script>
13 </head>
14 <body>
15
16 Calculate fibonacci:
17 <input type="text" id="input" value="10" />
18 <input type="button" id="calculate" value="Calculate">
19
20 <p>
21 Try entering values in the range of 10 to 50.
22 Verify that the browser stays responsive when working on a large calculation.
23 We have created 3 workers, so the worker pool will handle a maximum of three
24 tasks at a time. When exceeding this, tasks will be put in a queue.
25 </p>
26
27 <div id="results"></div>
28
29 <script>
30 // create a worker pool using an external worker script
31 var pool = workerpool.pool('../workers/crossWorker.js', {maxWorkers: 3});
32
33 var input = document.getElementById('input');
34 var calculate = document.getElementById('calculate');
35 var results = document.getElementById('results');
36
37 calculate.onclick = function () {
38 var n = parseInt(input.value);
39 var result = document.createElement('div');
40 result.innerHTML = 'fibonacci(' + n + ') = ... ';
41 results.appendChild(result);
42
43 var promise = pool.exec('fibonacci', [n])
44 .then(function (f) {
45 result.innerHTML = 'fibonacci(' + n + ') = ' + f;
46 })
47 .catch(function (error) {
48 result.innerHTML = 'fibonacci(' + n + ') = ' + error;
49 });
50
51 var a = document.createElement('A');
52 a.innerHTML = 'cancel';
53 a.href = '#';
54 a.onclick = function () {
55 promise.cancel();
56 };
57 result.appendChild(a);
58 };
59 </script>
60
61 </body>
62 </html>