JavaScript执行超时 [英] JavaScript execution exceeded timeout

查看:124
本文介绍了JavaScript执行超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 在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;
    })();


    1. I am not doing a large chunk of computation in JS.
    2. 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.");
          }
      }
      

    3. Then the web page worked for about 4 minutes and I get this error:

      JavaScript execution exceeded timeout.

    4. Then no JavaScript would load. None of the debug messages I inserted into my.js file would print. Only the above error.

    5. The error persists even after I left the page that generated this error, and opened other web pages under the same domain.

    6. 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆