I have a javascript script I’m developing for a phonegap app that gets device location until a certain target accuracy is obtained. If the target isn’t met the script calls itself again into eternity. Usually it takes between 1-8 attempts depending on location and the last time used and then everything works fine. The problem I’m having is that some devices never reach that threshold (which is fine for the app), but ruins battery life because this script keeps running in the background. So how can I kill this script with a click event (the submit button)? I was thinking about setting a global var that hitting the submit button changes. Something like:
if (var != 'quit') {
//function
} else {
///nothing
}
function getlocation(position) {
document.getElementById("lat").value = (position.coords.latitude);
document.getElementById("long").value = (position.coords.longitude);
document.getElementById("accu").value = (position.coords.accuracy);
var accuracy = parseInt(position.coords.accuracy, 10);
if (accuracy <= 50) {
$('#scanner').removeClass('littlered').addClass('littlegreen');
} else {
setTimeout(navigator.geolocation.getCurrentPosition(getDroplocation, onError, {
enableHighAccuracy: true
}), 3000);
}
}
This hasn’t been working though. I set the global var but nothing happens. Any better methods or ways of getting my aforementioned idea going? Am I on the right track?
Use clearTimeout() method clears a timer set with the setTimeout() method.