JavaScript执行超过了超时

YoY*_*Myo 6 javascript safari geolocation ipad

  1. 我在JS中没有做大量的计算.
  2. 在我添加此代码之前,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)
  3. 然后网页工作了大约4分钟,我得到这个错误:

    JavaScript执行超过了超时.

  4. 然后没有JavaScript会加载.我插入到my.js文件中的调试消息都不会打印出来.只有上面的错误.

  5. 即使在我离开生成此错误的页面并在同一域下打开其他网页后,错误仍然存​​在.

  6. 我用过try和catch,我使用了setTimeout函数,但是既没有给我错误的来源也没有解决问题.

我不知道问题是什么.它一整天都在燃烧我,并会在周末燃烧我.

YoY*_*Myo 6

令人惊讶的是,当我使用时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)