Created
December 25, 2016 16:12
-
-
Save nvurgaft/35ce383c51dbf7493db3f861a0faf12f to your computer and use it in GitHub Desktop.
A simple call and back using a webworker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.onload = function () { | |
console.info("Document loaded"); | |
} | |
var worker; | |
function runWorker() { | |
if (window.Worker) { | |
worker = new Worker("Worker.js"); | |
worker.onmessage = function (msg) { | |
document.querySelector("#resultarea").innerHTML = msg; | |
} | |
} else { | |
console.debug("Web workers are not supported"); | |
} | |
} | |
function sendMessage() { | |
if (worker) { | |
worker.postMessage("Aloha"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title></title> | |
<script src="app.js"></script> | |
</head> | |
<body> | |
<button onclick="runWorker()">Run worker job</button> | |
</br></br> | |
<button onclick="sendMessage()">Send message to worker</button> | |
</br></br> | |
<textarea name="" id="resultarea" cols="30" rows="10"></textarea> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onmessage = function(event) { | |
console.debug("posting message: ", event.data); | |
postMessage(event.data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment