I am working on a web-worker project. The web-worker is a small javascript file that determines the users GPS coordinates and sends the data back to the main page script every 5 seconds using postMessage.
I have tested the worker javascript alone and it seems to be working fine, I can get the GPS coordinates every 5 minutes and display them in an alert box (for testing) and I have tested the main page code with another worker script and it too seems to be working fine, but when I test the main page code with the worker script to get the GPS coordinates, nothing is received from the worker script.
Can anyone please help me figure out why this is not working and suggest a solution?
My goal is to have a gps worker script that runs continuously in the background and updates the user's gps position every 5 minutes, then passes that information back to the main page script where it will be displayed and stored in a mysql database.
Here is the main page script
<!DOCTYPE html>
<html>
<head>
</head>
<body onload="startWorker()">
<p>Your GPS Coordinates are:
<output id="result"></output>
</p>
<script>
var w;
function startWorker() {
if (typeof(Worker) !== "undefined") {
if (typeof(w) == "undefined") {
w = new Worker("x05.js");
}
w.onmessage = function(event) {
document.getElementById("result").innerHTML = event.data;
};
} else {
document.getElementById("result").innerHTML = "Sorry! No Web Worker support.";
}
}
</script>
</body>
</html>
and here is the worker file script
function getLocation()
{
navigator.geolocation.getCurrentPosition(getPosition);
}
function getPosition(position)
{
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var location = 'lat='+position.coords.latitude+'<br>long='+position.coords.longitude;
postMessage(location);
setTimeout(getLocation, 5000);
}
getLocation()