I have a problem with JavaScript where I need a function to run and complete before another function runs.
Here is the code I need to run and finish first. As you can see, I look at all the address input fields on the form and geocode 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) {
After that, it completes completely — that is, after all the Google Maps API responses have returned — I need a form to submit. Here is the current ajax input code that I use:
$('#form').ajaxForm( { success: function() { ... } } );
The problem I am facing is that the form is submitted before the Google Maps API responded. The ajaxForm() method ajaxForm() callback function, but it still does not beforeSubmit for the function to complete. I understand this is because JavaScript is asynchronous, but I'm not sure how to solve this problem.
Any help is appreciated! Thanks!
source share