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!