Issue with geolocation timeout callback firing whe

2019-05-29 20:01发布

I've implemented a timer for a HTML5 geolocation request as per this post Geolocation feedback while accepting the request

However I'm having an issue where the timer is called regardless of whether the navigator.geolocation.received response is true or not. Perhaps I'm missing something very obvious but I can't see what.

Basically, if I get the geo-location information I run the geo_success_action() function, but in every other case (geo-location failure, timeout on accepting location share, or non-existence of html5 geo-location support in the browser), I want to run the geo_failure_action() function.

However what's happening is that if geo-location is collected, my geo_success_action() function is called, and then when the timer runs out the geo_failure_action() is also called.

I had assumed that within var succeed, the setting of navigator.geolocation.received = true would be passed to my timedout function and therefore if navigator.geolocation.received was true, it wouldn't fire the resulting function.

Any thoughts?

var succeed = function(obj) {
    navigator.geolocation.received = true;
    if (!navigator.geolocation.timedout) {
        geo_success_action( obj, json_url );
    }
};
var failed = function(obj) { 
    navigator.geolocation.received = true;
    if (!navigator.geolocation.timedout) {
        geo_failure_action( json_url );
    } else {
        geo_failure_action( json_url );
    }
};
var timedout = function() {
    navigator.geolocation.timedout = true; // could be used for other callbacks to trace if its timed out or not
    if (!navigator.geolocation.received){
        geo_failure_action( json_url );
        //alert('Timed Out');
    } else {
        null;
    }
}

// Extend geolocation object
if ( navigator.geolocation  ) {
    navigator.geolocation.retrievePermission = function retrievePermission(succeed,failed,options,timeout) {
        this.received = false;              // reference for timeout callback
        this.timedout = false;              // reference for other callbacks
        this.getCurrentPosition.apply(this,arguments);  // actual request

        // Trigger timeout with its function; default timeout offset 5000ms
        if ( timeout ) {
            setTimeout(timeout.callback,timeout.offset || 5000);
        }
    }

    // New location request with timeout callback
    navigator.geolocation.retrievePermission(succeed,failed,{},{
        offset: 6000, // miliseconds
        callback: timedout  
    });

// If geo-location not supported at all, do failure action
} else {
    geo_failure_action( json_url );
}

1条回答
forever°为你锁心
2楼-- · 2019-05-29 20:40

I haven't tested, but this should work

var timedout = function() {
    navigator.geolocation.timedout = true; 

    if( navigator.geolocation.received === undefined ) {
        navigator.geolocation.received = true;
    }

    if (!navigator.geolocation.received){
        geo_failure_action( json_url );
        //alert('Timed Out');
    } else {
        null;
    }
}
查看更多
登录 后发表回答