title: unable to cope with the asynchronous nature of navigator.geolocation
body: <p>I'm using the navigator.geolocation.getCurrentPosition(function) api in firefox 3.6. When i try to call this method repeatedly I see that sometimes it works and sometimes it doesn't. I figured that the problem is because of its asynchronous callback nature. I can see that the callback function is being called at some point but my outer function is already exiting so i cannot catch the values of the position coordinates.</p>
  
  <p>I'm pretty new to javascript so i'm assuming other javascript coders might have already figured out how to deal with it. Please help.</p>
  
  <p>Edit: here's a sample piece of code i'm using  </p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
     function getCurrentLocation() {
       var currLocation;
        if(navigator.geolocation) {
           navigator.geolocation.getCurrentPosition(function(position) {
            currLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
          });
         }
         return currLocation; // this returns undefined sometimes. I need help here
  }    
  &lt;/script&gt;
  </code></pre>
  
  <p>Edit 2: 
  Thanks everyone for answering, I wish i could choose all the answers as "accepted" but cannot do so. </p>
  
  <p>Now I'm facing another problem. I'm calling the navigator.geolocation.getCurrentPosition every 3 seconds, but the responses stop after 10 - 15 replies. Any one got any idea ?</p>
  
  <p>thanks again </p>