title: Sequential JQuery Functions
body: <p>I'm experiencing a bug with my code when used on mobile devices (issue does not happen with the faster processing time of PCs). I suspect that the issue that I'm having is to do with functions not firing sequentially - ie they will still go ahead, even if all the parameters are not set.</p>
  
  <p>The basic premise is that JQuery Geolocation is called, and the latitude/longitude data is used in conjunction with a parameter passed via an onClick function. This is then passed into AJAX that comes back with results based off this data.</p>
  
  <p>During testing on mobile devices, however, sometimes when you click on links, the GPS won't fire in time, but it will still go ahead. The result is that nothing comes back and the user is faced with a blank screen. If you give it enough time and click enough, it will work. This doesn't always happen, but it appears that not all data is ready.</p>
  
  <p>I've tried a number of variations, but as I'm only an intermediate JS coder, I can't work out where the problem lies. Love some feedback!</p>
  
  <pre><code>if(navigator.geolocation) {/* Function for returning list of locations for a unique cuisine */
      function locationList(clicked_id) {
          navigator.geolocation.getCurrentPosition(
              function(position) { 
                  $.ajax({
                      url: 'search.php',
                      type: 'GET',
                      data: 'id=' + clicked_id + '&amp;action=geolocation&amp;latitude=' + position.coords.latitude + '&amp;longitude=' + position.coords.longitude,
                      beforeSend: function () {
                          $('#location-list').html('&lt;div class="ajax-load"&gt;&lt;img src="img/ajax-loader.gif" /&gt;&lt;/a&gt;');
                      },
                      success: function (msg) {
                          $('#location-list').html(msg);
                      },
                      error: function (XMLHttpRequest, textStatus, errorThrown) {   
                          alert('Error submitting request.'); 
                      }
                  });
              }, /* position */
              function() {
                  $('#location-list').html('&lt;div class="toolbar"&gt;&lt;h1&gt;GPS Error&lt;/h1&gt;&lt;a class="back" href="#home"&gt;Home&lt;/a&gt;&lt;/div&gt;&lt;div class="scroll"&gt;&lt;ul class="rounded"&gt;&lt;li&gt;&lt;p&gt;To take advantage of this app you need the GPS capability of your phone turned on. Please check whether this is turned on or else the app will not work&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;');
              } /* no position so failure */
          );
      } /* locationList */
  }/* navigator.geolocation */
  </code></pre>