Skip to content

Instantly share code, notes, and snippets.

@wtarr
Last active August 29, 2015 13:56
Show Gist options
  • Save wtarr/8862211 to your computer and use it in GitHub Desktop.
Save wtarr/8862211 to your computer and use it in GitHub Desktop.
Simple web worker example
<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>
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