我在接受请求的同时,根据这个后地理定位反馈,为HTML5地理定位请求实现了一个计时器
然而,我遇到了一个问题,无论navigator.geolocation.recipated响应是否为真,都会调用计时器。也许我错过了一些显而易见的东西,但我看不出是什么。
基本上,如果我得到了地理位置信息,我会运行geo_success_action()
函数,但在其他所有情况下(地理位置失败、接受位置共享超时或浏览器中不存在html5地理位置支持),我都想运行geo_failure_action()
函数。
然而,如果收集了地理位置,就会调用我的geo_success_action()
函数,然后当计时器用完时,也会调用geo_failure_action()
。
我曾假设,在var success中,navigator.geolocation.received = true
的设置将传递给我的timedout
函数,因此,如果navigator.geolocation.received
为true,它就不会触发生成的函数。
有什么想法吗?
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 );
}
我还没有测试,但这应该能在中工作
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;
}
}