Last active
August 29, 2015 13:56
-
-
Save wtarr/8862211 to your computer and use it in GitHub Desktop.
Simple web worker example
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>Web worker example</title> | |
</head> | |
<body> | |
<script> | |
var worker = new Worker("worker.js"); | |
worker.onmessage = function(e) { | |
//print the updated data | |
console.log(e.data); | |
}; | |
// post coords that are to be updated | |
worker.postMessage({message : "update", coords: {x: 1 , y: 2}}); | |
</script> | |
</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(e){ | |
if ( e.data.message === "update") | |
{ | |
// worker recieves an update message and updates the coordinates and | |
// replies back | |
postMessage( { newX : e.data.coords.x * 5, newY : e.data.coords.y * 5 }); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment