JavaScript执行超时 [英] JavaScript execution exceeded timeout
问题描述
在Safari中的iPad 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);
$ b} else {
console.debug(位置数据不够准确,位置未更新。);
然后,网页工作了大约4分钟,我得到这个错误:
JavaScript执行超时超时。 p>然后不加载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);
这比watchPosition更好,因为watchPosition有时不遵循规则(在3中更新位置8次如果超时仍然发生,则需要调用clearInterval:
var geolocationID;
(function getLocation(){
var count = 0;
geolocationID = window.setInterval(
function(){
count ++;
if(count > 3){//计数达到数字时,重置间隔
window.clearInterval(geolocationID);
getLocation();
} else {
navigator.geolocation.getCurrentPosition (updatePos,locationError,{enableHighAccuracy:true,timeout:10000});
}
},
600000); // end setInterval;
})();
- I am not doing a large chunk of computation in JS.
Everything works fine on iPad iOS 5 in Safari until I added this code:
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."); } }
Then the web page worked for about 4 minutes and I get this error:
JavaScript execution exceeded timeout.
Then no JavaScript would load. None of the debug messages I inserted into my.js file would print. Only the above error.
The error persists even after I left the page that generated this error, and opened other web pages under the same domain.
I used try and catch, I used setTimeout function, but neither gave me the source of the error nor solved the problem.
I don't know what the problem is. It has been burning me for the entire day and will be burning me for the weekend.
Surprisingly, when I used geolocation.getCurrentPosition
, the error message stopped showing up.
To replace watchPosition with getCurrentPosition, I use the function setInterval
:
function getLocation() {
navigator.geolocation.getCurrentPosition(updatePos, locationError, { enableHighAccuracy: true, timeout: 10000 });
}
var intervalID = window.setInterval(getLocation, 10000);
This has worked out better than watchPosition because watchPosition sometimes does not follow rules(update location 8 times in 3 minutes despite the frequency is set to 10 minutes).
If the timeout still occurs, one would need to call 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;
})();
这篇关于JavaScript执行超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!