JavaScript execution timed out

  • I do not do a big chunk of computing in JS.
  • Everything works fine on the 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 webpage worked for about 4 minutes and I get this error:

    JavaScript execution exceeded timeout.

  • Then loading JavaScript will not. None of the debug messages I pasted into my.js was printed. Only the above error.

  • The error persists even after I left the page that caused this error and opened other web pages in the same domain.

  • I used try and catch, I used the setTimeout function, but did not give me the source of the error and did not solve the problem.

I do not know what's the problem. He burned me all day and will burn me for the weekend.

+6
source share
6 answers

Surprisingly, when I used geolocation.getCurrentPosition , the error message stopped. To replace watchPosition with getCurrentPosition, I use the setInterval function:

  function getLocation() { navigator.geolocation.getCurrentPosition(updatePos, locationError, { enableHighAccuracy: true, timeout: 10000 }); } var intervalID = window.setInterval(getLocation, 10000); 

This turned out to be better than watchPosition, because watchPosition sometimes does not follow the rules (update location 8 times in 3 minutes, despite the fact that the frequency is set to 10 minutes).

If the timeout still occurs, you 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; })(); 
+6
source

Addition: This problem exists on the iPhone (not surprisingly) and has not yet been fixed. Even worse, if you exit (do not kill) the browser, you will get a battery discharger, the phone will be dead within 12 hours. Unfortunately, I need to rely on the clock, as getCurrentPosition is far from being so accurate.

As soon as an error is typed (after ~ 4 minutes, as reported), a run timeout error is reported exactly every second, and the JS engine is dead. The problem is that it is NOT a execution timeout. This is a mistake, even reloading the site does not change it, stopping / starting the clock does not help either. Only killing the browser (double clicking on the button, long clicking on the icon, then -) does the trick.

It seems like we're doomed until Apple releases the patch ...

Update: after additional experiments with this, it seems that the problem is with XHR queries, maybe with jquery specific, it seems that there are some restrictions on these queries or there is no proper cleaning. A timeout error will appear on any other site, as well as immediately after starting the first XHR. Before all JS is working fine.

+2
source

I struggled with the same problem and found a rather elegant solution.

Timeout errors occurred when I started my โ€œnext location modeโ€ with navigator.geolocation.watchPosition and reloaded the page without stopping it. Therefore, I could fix this problem by simply clearing the observer when the user leaves the page.

 $(window).unload(function(){ window.navigator.geolocation.clearWatch(myWatchHandler); }); 

Hope this helps!

+1
source

I use watchPosition to publish my long, latin and most important speed. I have the same problem as the users in this thread: Safari / Chrome on iOS killed the GPS channel (this was not the interval that was stopped, I tested). Only by killing the browser (as if you double-click the home button), I could make it work again for a couple of seconds. I tried everything like clearing the interval, but that didn't work. Then, later, I went outside and continued to work! He came back inside and the GPS signal disappeared. As applications like RunKeeper continued to work internally, I assumed that it would work internally as well, but that turned out to be wrong . If anyone has any questions, please feel free to ask.

+1
source

Perhaps this is a Safari problem that blocks all getCurrentPosition while watchPosition is running in another tab or website.

0
source

I had a similar problem and I also use html5 geolocation.

I have already implemented the proposed setInterval() solution. The same problem still occurred, although it took much longer.

I did some experimentation by putting the following code snippet in my code, just to โ€œpauseโ€ the code:

 ms = 30000; ms += new Date().getTime(); while (new Date() < ms){ } 

The above snippet reliably generates a javascript runtime. I discreetly discovered that the following snippet will generate a timeout. But it looks like the browser can restore the timeout. Do not ask me why. It just works. I hope that the victims will be able to verify my findings.

 ms = 30000; ms += new Date().getTime(); while (new Date() < ms){ if (new Date() %100 == 0){ console.log('in while'); } } 

So, the solution seems trivial, it might seem that console.logs should be added to the parts of the code that gets called most often. I hope this can be useful (although I think it is funny too, but it works for me)

0
source

Source: https://habr.com/ru/post/899832/


All Articles