I have below code in main.html
if(typeof(Worker) !== "undefined") {
if(typeof(w) == "undefined") {
w = new Worker("/location_worker.js");
}
w.onmessage = function(event) {
sequence = event.data;
if(Cookies.get("location_tracked") == "done")
{
w.terminate();
w = undefined;
return;
}
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(function(position)
{
lat = position.coords.latitude;
lon = position.coords.longitude;
$.ajax({
type: "POST",
url: "/api/",
data: {'action':'receive_loc', 'lat': lat,'lon': lon,'enc_data': enc_data,'reached': reached , 'sequence' : sequence },
success: function(jres)
{
res = JSON.parse(jres);
stop = false;
if(res.status == 11)
{
stop = true;
}
if(!stop)
{
//loop continues
}else{
finished = true;
w.terminate();
w = undefined;
return;
}
},
});
},function(code, message) {
$('#error').html("<b>Location Sharing blocked by User..</b>");
});
} else{
alert("Sorry, your browser does not support HTML5 geolocation.");
}
};
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Workers...";
}
And the location_worker.js
contains below
var i = 0;
var ft = 2000;
function timedCount() {
i = i + 1;
self.postMessage(i);
console.log(i);
setTimeout("timedCount()",ft);
}
timedCount();
so for every 2 seconds the ajax will be submitted which is working fine but in below scenarios its not working as expected
for example if sequence 1 and 2 worked fine and tab/browser got inactive and after sometime if i return back to the this page, all the subsequent requests (which are supposed to be sent when tab is inactive) are getting sent at same time (sequence 3,4,5,6,7,8,9 etc).so all these requests are sending same data with same lat lon values which is not supposed to be happened.
someone please let me know what is wrong with this flow.