问题与地理定位超时回调触发是否收到响应 [英] Issue with geolocation timeout callback firing whether response received or not

查看:125
本文介绍了问题与地理定位超时回调触发是否收到响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据这篇文章为HTML5地理定位请求实现了一个计时器地理定位反馈,同时接受请求



然而,我遇到了一个问题,无论navigator.geolocation.received响应是否被调用真实与否。基本上,如果我得到地理位置信息,我会运行或者 geo_success_action()函数,但在其他情况下(地理位置失败,接受位置共享超时或浏览器中不存在html5地理位置支持),我想运行 geo_failure_action()函数。

然而,如果收集到地理位置,我的 geo_success_action()函数被调用,然后当计时器用完时< geo_failure_action()也被调用。



我假设在var成功的情况下, navigator.geolocation.received = true 的设置将被传递给我的 timedout 函数,因此如果 navigator.geolocation.received 为真,它不会触发结果函数。



有什么想法?

  var succeed = function(o bj){
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; //可用于其他回调追踪,如果超时或不
如果(!navigator.geolocation.received){
geo_failure_action(json_url);
// alert('Timed Out');
} else {
null;



//扩展地理定位对象
if(navigator.geolocation){
navigator.geolocation.retrievePermission = function retrievePermission(success,failed ,options,timeout){
this.received = false; //超时回调引用
this.timedout = false; //引用其他回调
this.getCurrentPosition.apply(this,arguments); //实际请求

//使用其功能触发超时;默认超时偏移5000ms
if(timeout){
setTimeout(timeout.callback,timeout.offset || 5000);



//带有超时回调的新位置请求
navigator.geolocation.retrievePermission(成功,失败,{},{
偏移量: 6000,//毫秒
回调:超时
});

//如果根本不支持地理位置,请执行失败操作
} 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;
}
}


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

解决方案

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

这篇关于问题与地理定位超时回调触发是否收到响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆