YoY*_*Myo 6 javascript safari geolocation ipad
在我添加此代码之前,Safari iOS iOS 5上的一切正常工作:
var watchID = navigator.geolocation.watchPosition(updatePos,locationError,{maximumAge: 10000, frequency: 60000, enableHighAccuracy: true, timeout: 1000});
function updatePos(position) {
if (position.coords.accuracy < accuracyThreshold) {
$.post(websiteRoot + '/ReportDeviceLocation?rand=' + (Math.random() * Math.random()) + '&longitude=' + position.coords.longitude + '&latitude=' +position.coords.latitude);
} else {
console.debug("Location data is not accurate enough. Location not updated.");
}
}
Run Code Online (Sandbox Code Playgroud)然后网页工作了大约4分钟,我得到这个错误:
JavaScript执行超过了超时.
然后没有JavaScript会加载.我插入到my.js文件中的调试消息都不会打印出来.只有上面的错误.
即使在我离开生成此错误的页面并在同一域下打开其他网页后,错误仍然存在.
我用过try和catch,我使用了setTimeout函数,但是既没有给我错误的来源也没有解决问题.
我不知道问题是什么.它一整天都在燃烧我,并会在周末燃烧我.
令人惊讶的是,当我使用时geolocation.getCurrentPosition
,错误消息停止显示.要用getCurrentPosition替换watchPosition,我使用以下函数setInterval
:
function getLocation() {
navigator.geolocation.getCurrentPosition(updatePos, locationError, { enableHighAccuracy: true, timeout: 10000 });
}
var intervalID = window.setInterval(getLocation, 10000);
Run Code Online (Sandbox Code Playgroud)
这比watchPosition效果更好,因为watchPosition有时不遵循规则(尽管频率设置为10分钟,但在3分钟内更新位置8次).
如果仍然发生超时,则需要调用clearInterval:
var geolocationID;
(function getLocation() {
var count = 0;
geolocationID = window.setInterval(
function () {
count++;
if (count > 3) { //when count reaches a number, reset interval
window.clearInterval(geolocationID);
getLocation();
} else {
navigator.geolocation.getCurrentPosition(updatePos, locationError, { enableHighAccuracy: true, timeout: 10000 });
}
},
600000); //end setInterval;
})();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5941 次 |
最近记录: |