Issue with geolocation timeout callback firing whether response received or not

二次信任 提交于 2019-12-08 04:18:35

问题


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:


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;
    }
}


来源:https://stackoverflow.com/questions/9738167/issue-with-geolocation-timeout-callback-firing-whether-response-received-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!