[英]JavaScript - clearWatch() not triggering on setTimeout()
I'm encountering some issues with JavaScript's watchPosition and clearWatch functions that I don't quite understand, would appreciate it if someone could help out. 我遇到了JavaScript的watchPosition和clearWatch函数的一些问题,我不太明白,如果有人可以提供帮助,我会很感激。
First off, the function in question... 首先,有问题的功能......
function location(x){
var getLocation;
var lat_input = $(x).find(".latitude");
var lon_input = $(x).find(".longitude");
var acc_input = $(x).find(".accuracy");
function showPosition(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
accuracy = position.coords.accuracy;
$(lat_input).val(latitude);
$(lon_input).val(longitude);
$(acc_input).val(accuracy);
setTimeout(function(){
navigator.geolocation.clearWatch(getLocation);
alert('done');
}, 10000);
};
getLocation = navigator.geolocation.watchPosition(showPosition, null, {maximumAge: 0, timeout: Infinity, enableHighAccuracy: true});
};
In particular, my problem is with the code in the setTimeout section. 特别是,我的问题是setTimeout部分中的代码。
When I run this function on my laptop, everything starts out ok - the .longitude, .latitude, and .accuracy fields are populated accordingly. 当我在笔记本电脑上运行此功能时,一切都开始正常 - 相应地填充.longitude,.latitude和.accuracy字段。 After 10 seconds, setTimeout is triggered - I can't say if clearWatch runs successfully (since the computer is pretty limited in its ability to find itself), but the odd behavior that I note here is that alert('done')
is triggered twice. 10秒后,触发了setTimeout - 我不能说clearWatch是否成功运行(因为计算机的查找能力非常有限),但我在这里注意到的奇怪行为是触发alert('done')
两次。
When I run this function on my phone (the intended platform), everything gets off to a similarly good start, but after 10 seconds, alert('done')
starts triggering endlessly, sometimes in immediate succession, sometimes with a few seconds in between. 当我在我的手机(预定平台)上运行此功能时,一切都开始了同样良好的开始,但是在10秒后, alert('done')
开始无休止地触发,有时立即连续触发,有时几秒钟之间。 Most distressing, however, is that clearWatch doesn't appear to run at all - that lat, long, and accuracy fields continuously update after the 10 second mark. 然而,最令人痛苦的是,clearWatch似乎根本没有运行 - lat,long和precision字段在10秒标记后不断更新。
If anyone can see what I am doing wrong here, your guidance would be much appreciated. 如果有人能在这里看到我做错了什么,那么您的指导将非常感激。
I hit a similar issue. 我遇到了类似的问题。 I found the following code did not stop the watch: 我发现以下代码没有停止监视:
navigator.geolocation.clearWatch(locationID);
STEP 1: Create a variable and assign it to navigator.geolocation 步骤1:创建变量并将其分配给navigator.geolocation
var geoLoc = navigator.geolocation;
var geoWatchID = geoLoc.watchPosition(
onGPSSuccess, onGPSError,
{ maximumAge: 300, timeout: 1500, enableHighAccuracy: true });
STEP 2 : Stop Watch use the var geoloc not navigator.geolocation.clearWatch() 第2 步 :Stop Watch使用var geoloc not navigator.geolocation.clearWatch()
geoLoc.clearWatch(geoWatchID);
geoWatchID = null;
See http://www.tutorialspoint.com/html5/geolocation_clearwatch.htm 请参阅http://www.tutorialspoint.com/html5/geolocation_clearwatch.htm
Without knowing the libraries to re-create the code, I can't duplicate the issue so I can only give something to try. 在不知道库重新创建代码的情况下,我无法复制问题,所以我只能尝试一些东西。
Add a timer
variable to your outer scope. 将timer
变量添加到外部作用域。
clearTimeout(timer);
timer = setTimeout(function(){
navigator.geolocation.clearWatch(getLocation);
alert('done');
}, 10000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.