Game Development Reference
In-Depth Information
uls[i].remove();
// Display the results
document.body.appendChild(ul);
});
var input = document.createElement("input");
input.addEventListener("keyup", function(event){
var key = event.which;
// Call the worker when the Enter key is
pressed
if (key == 13 /* Enter */) {
var input = this.value;
// Only use input that's a positive number
if (!isNaN(input) && input > 0) {
worker.postMessage({max: input});
} else if (input == -1) {
worker.terminate();
this.remove();
}
}
});
input.setAttribute("autofocus", true);
document.body.appendChild(input);
In the above snippet, we set up two things: a worker and an input field. We then set
up a keydown listener on the input field, which we use so the user can input a num-
ber to send to the worker. To send this number to the worker, the user must press
the Enter key. When that happens, the number in the input field will be the highest
possible prime number generated by the worker. If the user inputs the number -1 ,
the worker is terminated, and the input field is removed from the DOM.
For simplicity, the worker thread will use the Sieve of Eratosthenes to find the
primes. Keep in mind that this exercise is only a proof of concept to illustrate how
web workers work, and not a lesson on advanced mathematics.
Search WWH ::




Custom Search