d628147bca
- Prime number background worker that can be started, stopped and reset that calculates prime numbers and sends them back to the main page/thread.
41 lines
1.2 KiB
HTML
41 lines
1.2 KiB
HTML
<!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>
|