Stop execution of getCurrentPosition method

I am using the getCurrentPosition method in Javascript. I would like to implement a button that stops the "getCurrentPosition" method when clicked. I tried using throw / try / catch blocks, but it does not 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 this possible in JS? I had an idea, but I don’t know if it is possible to start a timeout using the JS method to getCurrentPosition break?

+5
source share
1 answer

Decision:

The navigator.geolocation.getCurrentPosition method is an asynchronous function, so don't count on stopping it the exact same way.

Instead, you should use another function: navigator.geolocation.watchPosition . It works the same as getCurrentPosition, but what makes it useful in your case is another function called navigator.geolocation.clearWatch and this function is used to stop watchPosition.

Example:

geoLoc = navigator.geolocation;
watchID = geoLoc.watchPosition(showLocation, errorHandler, options);
geoLoc.clearWatch(watchID);
+10
source

All Articles