Answer the question
In order to leave comments, you need to log in
I am studying how the Web Workers API works and the first simple script using it does not work. What am I doing wrong?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ping Pong</title>
<script src="manager.js"></script>
</head>
<body>
<p id="output"></p>
</body>
</html>
//manager.js
window.onload = function() {
var worker = new Worker("worker.js");
worker.postMessage("ping");
worker.onmessage = function (event) {
var message = "Worker says " + event.data;
document.getElementById("output").innerHTML = message;
};
}
//worker.js
onemessage = pingPong;
function pingPong(event) {
if (event.data == "ping") {
postMessage("pong");
}
}
Answer the question
In order to leave comments, you need to log in
The error was in the worker.js file. Instead of "onemessage" write "onmessage".
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question