navigator.geolocation.getCurrentPosition Callbacks won't work on Firefox 10

后端 未结 2 1510
情话喂你
情话喂你 2021-02-05 14:57

I am building an app that uses the Geolocation API. I cant seem to get a very simple piece of code to work on Firefox 10. Here is the code:

    window.onload = f         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-05 15:16

    All right I found that the problem is indeed Firefox and that it does not work reliably or equally on all platforms. Looking at http://dev.w3.org/geo/api/spec-source.html I found the following option to add:

        window.onload = function() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function(position) {
                    alert('it works');
                }, function(error) {
                    alert('Error occurred. Error code: ' + error.code);         
                },{timeout:5000});
            }else{
                alert('no geolocation support');
            }
        };
    

    As you can see here the timeout:5000 has been added which means that if for some reason the browser takes more then 5000ms (5 seconds) then throw a timeout error (that's error code 3). So now whenever Firefox is not working it at least runs the error callback and i get an alert message of "Error occurred. Error code: 3".

    Apparently the default value of timeout is infinite so it never times out... Chrome is 100% reliable but Firefox is about 10% reliable on my machine which is very disappointing. On my other computer which is running windows XP and is on the same network, Firefox seems to be 100% reliable.

提交回复
热议问题