4

I'm having a JavaScript issue where I need a function to run and complete before another function is run.

Here's the code I need to run and complete first. As you can see, I'm looping through all address input fields in the form and geocoding them through the Google Maps API.

  $('#form input:text.address').each(function() {
    var address = $(this);
    var Geocoder = new google.maps.Geocoder();

    Geocoder.geocode({ 'address': address.val() }, function(results, status) {
      // Store the results in a hidden input field.
    });
  });

After this fully completes — that is, after all the responses from the Google Maps API have returned — I then need the form to submit. Here's the current ajax submit code I use:

  $('#form').ajaxForm(
    {
      success:
        function() {
          ...
        }
    }
  );

The problem I'm having is the form is submitted before the Google Maps API has responded. The ajaxForm() method allows a beforeSubmit callback function, but that still does not wait for the function to complete. I realize this is because JavaScript is asynchronous, but I'm unsure how to solve this particular issue.

Any help is appreciated! Thanks!

2
  • 1
    I'm not sure if you want to be re-creating the geocoder every time you loop through the each function.
    – Chris B
    Oct 21, 2009 at 20:57
  • Yeah, I think that's a good call. Thanks, Chris B. Oct 21, 2009 at 21:22

3 Answers 3

7

Call the form submit from the success handler of Geocoder.geocode. Since you have many calls to the same function, set an indicator to indicate when the last call is finished and then submit the form.

Here is one way...

var count = $('#form input:text.address').size();
$('#form input:text.address').each(function() {
    var address = $(this);
    var Geocoder = new google.maps.Geocoder();

    Geocoder.geocode({ 'address': address.val() }, function(results, status) {
      // Store the results in a hidden input field.
      count--;
      if (count === 0) {
          //submit form here!
      }
    });
  });
0
0

I don't know JQuery, so please forgive me if I'm off base, but it seems to me that you can solve your problem by submitting the form inside the geocode results handler (where it says // Store the results in a hidden input field.) instead of from that other success handler (which I gather is supposed to be called when an AJAX query succeeds?). Is there something preventing you from doing it that way?

0

The problem is that the form can still be submitted before all Google-functions have loaded their data? Then we need to disable the submit buttons from the start, load all the data, and then enable the forms again after it has completed loading.

Uing chetan-sastry's solution to check for when form has loaded things should look something like this.

var count = $('#form input:text.address').size();

$('#form input:text.address').each(function() {
  var address = $(this);
  var Geocoder = new google.maps.Geocoder();

  Geocoder.geocode({ 'address': address.val() }, function(results, status) {
    // Store the results in a hidden input field.

      count--;
      if (count === 0) {
         $('#form').ajaxForm( /* ... */);
         $('#form input:submit').attr('disabled', '0'); // Enable submit-buttons
      }
  });
});

Then to prevent the user to submit the form before stuff has been loaded you should set the submit to disabled when the page is loading.

<input type="submit" disabled="1" />
1
  • ah, I see. Maybe you should clear that out in your second code sample, that you are running your first code sample inside that one :) Oct 21, 2009 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.