Javascript 如何在用户授权后启动navigator.geolocation超时计时器?

Javascript 如何在用户授权后启动navigator.geolocation超时计时器?,javascript,jquery,geolocation,Javascript,Jquery,Geolocation,正如您所知,HTML地理位置必须由用户通过浏览器授权。我运行了5秒的超时,并注意到如果用户需要更长的时间来单击接受(或拒绝),那么它将停止工作。有没有办法让计时器在用户单击接受/拒绝后启动,而不是在调用函数时启动 我这样称呼它: navigator.geolocation.getAccurateCurrentPosition( showPosition, showError, showProgress, {desiredAccuracy:1500, maxW

正如您所知,HTML地理位置必须由用户通过浏览器授权。我运行了5秒的超时,并注意到如果用户需要更长的时间来单击接受(或拒绝),那么它将停止工作。有没有办法让计时器在用户单击接受/拒绝后启动,而不是在调用函数时启动

我这样称呼它:

navigator.geolocation.getAccurateCurrentPosition(
         showPosition, showError, showProgress, 
         {desiredAccuracy:1500, maxWait:5000}
);
以及功能:

navigator.geolocation.getAccurateCurrentPosition = function (showPosition, showError, showProgress, options) {
    var lastCheckedPosition,
        locationEventCount = 0,
        watchID,
        timerID;

    options = options || {};

    var checkLocation = function (position) {
        lastCheckedPosition = position;
        locationEventCount = locationEventCount + 1;
        // We ignore the first event unless it's the only one received because some devices seem to send a cached
        // location even when maxaimumAge is set to zero
        if ((position.coords.accuracy <= options.desiredAccuracy) && (locationEventCount > 1)) {
            clearTimeout(timerID);
            navigator.geolocation.clearWatch(watchID);
            foundPosition(position);
        } else {
            showProgress(position);
        }
    };

    var stopTrying = function () {
        navigator.geolocation.clearWatch(watchID);
        foundPosition(lastCheckedPosition);
    };

    var onError = function (error) {
        clearTimeout(timerID);
        navigator.geolocation.clearWatch(watchID);
        showError(error);
    };

    var foundPosition = function (position) {
        showPosition(position);
    };

    if (!options.maxWait)            options.maxWait = 5000; // Default 5 seconds
    if (!options.desiredAccuracy)    options.desiredAccuracy = 1500; // Default 1500 meters
    if (!options.timeout)            options.timeout = options.maxWait; // Default to maxWait

    options.maximumAge = 0; // Force current locations only
    options.enableHighAccuracy = true; // Force high accuracy (otherwise, why are you using this function?)

    watchID = navigator.geolocation.watchPosition(checkLocation, onError, options);
    timerID = setTimeout(stopTrying, options.maxWait); // Set a timeout that will abandon the location loop
};
navigator.geolocation.getAccurateCurrentPosition=函数(显示位置、淋浴ROR、显示进度、选项){
var lastCheckedPosition,
locationEventCount=0,
watchID,
timerID;
选项=选项| |{};
var checkLocation=功能(位置){
lastCheckedPosition=位置;
locationEventCount=locationEventCount+1;
//我们忽略第一个事件,除非它是唯一一个接收到的事件,因为某些设备似乎发送缓存的消息
//位置,即使maxaimumAge设置为零
如果((位置坐标精度1)){
清除超时(timerID);
navigator.geolocation.clearWatch(watchID);
职位(职位);
}否则{
显示进度(职位);
}
};
var stopTrying=函数(){
navigator.geolocation.clearWatch(watchID);
foundPosition(上次检查的位置);
};
var onError=函数(错误){
清除超时(timerID);
navigator.geolocation.clearWatch(watchID);
错误;
};
变量foundPosition=函数(位置){
showPosition(位置);
};
如果(!options.maxWait)options.maxWait=5000;//默认值为5秒
如果(!options.desiredAccuracy)options.desiredAccuracy=1500;//默认值1500米
如果(!options.timeout)options.timeout=options.maxWait;//默认为maxWait
options.maximumAge=0;//仅强制当前位置
options.enableHighAccurance=true;//强制高精度(否则,为什么要使用此函数?)
watchID=navigator.geolocation.watchPosition(checkLocation,onError,options);
timerID=setTimeout(stopTrying,options.maxWait);//设置将放弃位置循环的超时
};
jsfiddle上的代码:jsfiddle上的代码: