I have implemented HTML Geolocation with a 10 second timeout. However, the 'failure' callback is not being triggered after 10 seconds. (The 'failure' callback is called if the user rejects permission to share his position).
What is wrong with my code?
Here is my code:
function getLocation(){
var deferred = $q.defer();
var success = function(position){
deferred.resolve(position);
};
var failure = function(error){
deferred.reject(error);
};
var options = {timeout: 10000};
if ($window.navigator && $window.navigator.geolocation) {
$window.navigator.geolocation.getCurrentPosition(success, failure, options);
}
else {
deferred.reject("Unsupported browser");
}
return deferred.promise;
}
SOLVED
The problem is the geolocation's own timeout is only started when the user agrees to share his location. I wanted to deal with a situation where the user doesn't agree and no callback is triggered. The solution is to create another timeout using $timeout.