mirror of
https://github.com/novnc/noVNC.git
synced 2026-06-07 21:04:37 +00:00
WebWorkers example with two way messages.
- Prime number background worker that can be started, stopped and reset that calculates prime numbers and sends them back to the main page/thread.
This commit is contained in:
40
prime.html
Normal file
40
prime.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>Worker example: One-core computation</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>The highest prime number discovered so far is: <output id="result"></output></p>
|
||||
<input type='button' value="Start" onclick="worker.postMessage('start');">
|
||||
<input type='button' value="Stop" onclick="worker.postMessage('stop');">
|
||||
<input type='button' value="Reset" onclick="worker.postMessage('reset');">
|
||||
<br>
|
||||
Log:<br>
|
||||
<textarea id='debug' style="font-size: 9;" cols=80 rows=25></textarea>
|
||||
<script src="include/mootools.js"></script>
|
||||
<script src="include/mootools-more.js"></script>
|
||||
<script>
|
||||
function debug(str) {
|
||||
cell = $('debug');
|
||||
cell.innerHTML += str + "\n";
|
||||
cell.scrollTop = cell.scrollHeight;
|
||||
}
|
||||
|
||||
debug('Starting');
|
||||
|
||||
var worker = new Worker('prime.js');
|
||||
worker.onmessage = function (event) {
|
||||
var cmd = event.data.substr(0,4);
|
||||
var data = event.data.substr(4);
|
||||
switch (cmd) {
|
||||
case 'log:':
|
||||
debug(data);
|
||||
break;
|
||||
case 'num:':
|
||||
$('result').innerHTML = data;
|
||||
break;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user