Skip to content

Instantly share code, notes, and snippets.

@nvurgaft nvurgaft/Worker.js
Created Dec 25, 2016

Embed
What would you like to do?
A simple call and back using a webworker
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");
}
}
<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>
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
You can’t perform that action at this time.