I'm using the getCurrentPosition method in Javascript. I would like to implement a button that stops the execution of the method "getCurrentPosition" when it's clicked. I tried with throw/try/catch blocks but it doesn't seem to work :
try { $('#cancel').on("click", function() { $.mobile.loading('hide'); throw "stop"; }); navigator.geolocation.getCurrentPosition(foundLocation, noLocation, {enableHighAccuracy: true, timeout : 30000 }); } catch ( er ) { alert(er); return false; }
Any ideas? Is it even possible in JS ? I had an idea but I don't know if it's possible to trigger a timeout with a JS method so that the getCurrentPosition breaks ?
Method navigator.geolocation.getCurrentPosition is an asynchronous function so don't count on stopping it just like that.
You should instead use other function called: navigator.geolocation.watchPosition. It works in the same way as getCurrentPosition but what make it usefull in your case is another function called navigator.geolocation.clearWatch and that function is used to stop watchPosition.
geoLoc = navigator.geolocation;
watchID = geoLoc.watchPosition(showLocation, errorHandler, options);
geoLoc.clearWatch(watchID);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With