0

I have this code, and for some reason my $.post function fires twice in quick succession (for 'journal/weather') according to Firebug. It still does this when I remove the "if (navigator.geolocation)", however, if I replace the $.post block with something like console.log('test'), it only fires once.

What's even weirder is when I place console.log('test') before the $.post function, then my event only fires once as well. I am assuming something is going on strange with jQuery. Anybody have any ideas?

if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(function(position)
        {
        $.post('journal/weather', {latitude: position.coords.latitude, longitude: position.coords.longitude}, function(result)
            {
            if (typeof(result.current.temp) != 'undefined')
                {
                global.temperature = parseInt(result.current.temp, 10);
                global.temperature_slider.slider('value', global.temperature);
                }
            }, "json");
        });
    }

EDIT: Okay, stranger still, sometimes it fires twice, sometimes once. How can the same piece of code give different results each time it is executed?

EDIT 2: After some experimentation, I think this is a bug in Firefox. Why? Because it happens not only with my page, but others. If I keep refreshing my page, roughly the first 10 times the navigator.geolocation.getCurrentPosition fires the callback function. However, at a point, it quits doing so. And once it reaches this point, it doesn't work for any other website that uses getCurrentPosition. And this problem never happens on Chrome or even IE. Searching Google, I found this:

http://groups.google.com/group/mozilla.feedback.firefox/browse_thread/thread/fecc3fb0bad6d0b8

nwalke
  • 3,170
  • 6
  • 35
  • 60
Nick
  • 5,228
  • 9
  • 40
  • 69
  • It might help if you told how this code block was getting called. And the problem may be in your use of `getCurrerentPosition` check this article http://stackoverflow.com/questions/2707191/unable-to-cope-with-the-asynchronous-nature-of-navigator-geolocation. – qw3n Jul 16 '10 at 18:29
  • I believe I am using getCurrentPosition correctly.. at least according to the answers in the link you provided. However, get this. I put this code into the Firebug command line on some random webpage (any webpage): navigator.geolocation.getCurrentPosition(function() { alert('Position obtained'); }); Nothing happens for like 40 seconds, and then my message appears. And I just tried Chrome and I am not getting this problem at all. – Nick Jul 16 '10 at 18:37

1 Answers1

0

It sounds like the code may be being called before the page is finished loading - hence console.log perhaps not yet being initialized.

Is your code code wrapped in a document.ready of some sort, e.g.

$(function () { if (navigator.geolocation) { ... } });

Just a guess. Would need to know more about the context in which the code appears to have a better stab at it.

Hope that helps and good luck.

Brian M. Hunt
  • 81,008
  • 74
  • 230
  • 343
  • The code is part of a queue of functions, and is called once (actually quite a bit after) the document is ready. – Nick Jul 16 '10 at 18:28