Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3346318
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:17:21+00:00 2026-05-18T01:17:21+00:00

ok i have an ajax call that recives a json object. the idea of

  • 0

ok i have an ajax call that recives a json object.

the idea of whatn i am trying to do is get a list of all records flagged, and geocode them do a reverse lookup and save back to the database, but as a batch

I want to loop through the data and then perform an additional ajax call (which will geocode the data) and then perform another ajax call to save to the database.

the problem is in my for loop everything runs too quickly and there is no pause between the calls and the browser crashes, or the save to database function adds the wrong data.

$('#geo_batch').click(function (){
            var ajax_load = "<label><img src='/images/icons/loadinfo.gif' alt='saving location...' /> Loading data...</label>";
            $("#batch_detail").html(ajax_load);
            $('#batch_buttons').hide();
            saveall = true;
            var form = $("form"); //Grab the form element from the DOM 
            //alert(form.serialize());
            var mydata = form.serialize();
            $.ajax({  
                type: "POST",  
                url: 'geo_getupdate_list.php', 
                data: mydata,  
                dataType: 'json',
                success: function(dat) {
                    processbatch(dat);// process the returned data
                }, 
                error: function(dat) { //Triggered if an error communicating with server   
                     //alert('fail');
                     $("#batch_detail").html('<label>There was an error: '+dat+'<label>');  
                     $('#batch_buttons').show();
                }
            });  
            return false; //Ignore the default behavior of the button click  
        });

the process data function

function processbatch(dat){
            // Cache the batch_detail element
            $("#batch_detail").html('<label>Locations have been retrieved:<br>' + dat + '<label>');
            $('#batch_buttons').show();
            var count = dat.location.length - 1;
            for(i=0; i < count; i++){
                $('#batch_detail').append('<li>address: ' + dat.location[i].geoaddr_mdt + 'flag: ' + dat.location[i].flag_mdt+'</li>');
                $('#id_mdt').val(dat.location[i].id_mdt);
                $('#entrytype').val(dat.location[i].idedt_mdt);
                $('#name').val(dat.location[i].name_mdt);
                $('#geo_addr').val(dat.location[i].geoaddr_mdt);
                $('#telephone').val(dat.location[i].telephone_mdt);
                $('#email').val(dat.location[i].email_mdt);
                $('#geo_detail').val(dat.location[i].displayaddr_mdt);
                $('#website').val(dat.location[i].website_mdt);
                //$('#active').val(dat.location[i].active_mdt);
                var address = dat.location[i].geoaddr_mdt;
                // if address is not empty
                if(address != '') {
                    address_lookup(address, region, 'update');
                };
            };
        };

address lookup function

function address_lookup(address, region, savetype) {
        // set default region
        if(region==null || region == '') {
            region = 'uk';
        };
        // address not empty
        if(address != '') {
            //clear existing markers<br />
            if(savetype == 'save'){
                removemarkers();
            };
            $('#geo_detail').html('<label>Geocoding address...</label>');
            // lookup the address
            geocoder.geocode( {'address':address,'region':region}, function(results, status) {
                // if the address was found
                if(status == google.maps.GeocoderStatus.OK) {
                    $str = '<label>Geocode Successful<br> Lattitude: '+results[0].geometry.location.lat()+' Longitude: '+results[0].geometry.location.lng()+'<br> The address is displayed below and will be stored in the database.<br> If the address is incorrect you may edit it before saving the location to the database.<br>If the marker is in the wrong location you may drag it to where you  believe it should be.</label>';
                    $('#geo_detail').html($str);
                    // insert lat/long into form
                    $('#lat').val(results[0].geometry.location.lat());
                    $('#lng').val(results[0].geometry.location.lng());
                    // create new lat/long object
                    latlng = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng());
                    $('#disp_addr').val(address);
                    $('#form_buttons').show();
                    $('#detail_address').show();
                    //reverselookup(results[0].geometry.location.lat(), results[0].geometry.location.lng());
                    // set zoom option
                    map.setZoom(15);
                    // center the map on the new location
                    map.setCenter(results[0].geometry.location);
                    createMarker(map, latlng, true, false);
                    if(savetype ='update'){
                        savedata('update');
                    };
                    if(savedata='save'){
                        savedata('save');
                    };
                } else {
                    // display error
                    $('#geo_detail').append('<label>Geocoder failed to retrieve address: '+status+'</label>');
                    $('#disp_addr').val($('#geo_addr').val());
                };
            });
        };
    };

EDIT in reply to Zacks 1st comment—-

i want to show each result as it is being processed, then output the result to the geo_detail div so there is a list of the processed record and associated error messages that are returned, idealy i would like the option to have the user review each record as it is processed, so the loop gets paused until the user clciks the save button, which would perhaps set a global var to true which my loop would check and wait for, b ut to start with i need some kind of pause so that the various ajax calls have time to execute before processing the next record.

END EDIT——————————
any ideas or pointers would be much appreciated

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:17:22+00:00Added an answer on May 18, 2026 at 1:17 am

    I don’t think “everything runs too quickly” is an accurate diagnosis of whatever problem you are having.

    One thing I see is that you have ajax calls being initiated from the success fn of other ajax calls. I don’t know if the ajax lib is re-entrant. You may want to avoid that.

    The way I would do it is to introduce asynchrony into the mix. Use setTimeout() to invoke your processbatch() function. It will then run asynchronously, with respect to the first ajax call.

    Also consider using asynchrony to invoke the goecoder, as well. Only send a new geocoder request after all other geocoder requests have completed. The way you do it, you send them all at once. (Maybe this is what you mean by “everything runs too quickly”).

    The way to do this: accept into your address_lookup fn an array of address objects and an index. Invoke the geocoder on the i’th address. Then when the call completes (success or failure), increment the index, and use setTimeout to invoke address_lookup again.

    This way you always have at most one outstanding call to the geocoder.

    It looks something like this in code:

    var delay = 140;
    function doAllAddresses(a, ix, callback) {
    
        // handle any addresses.
        // Test the list index for validity. If valid, 
        // Do the geocode, then increment the list index,
        // and call "myself" to handle additional 
        // addresses. When no more addresses remain, invoke
        // the 'done' callback.
    
        if (ix < a.length) {
            invokeGeocoderForOneAddress(a[ix], function() {
              // success function - gets invoked only when call to geocoder returns
              ix++;
              setTimeout(function(){doAllAddresses(ltr, ix, callback);}, delay);
            });
            return;
        }
    
        callback();
    }
    
    // kickoff:
    doAllAddresses(addresses, 0, function() { alert("all addresses are done.");}); 
    

    EDIT
    ps: I recommend you fixup your fn naming to use camelCase and verbNoun naming. So, processBatch() and lookupAddress(), rather than processbatch() and address_lookup().


    EDIT
    Here’s how to think about it: when you invoke an ajax call, you’re telling the browser to send out an HTTP request. Normally at the time you invoke the call you pass a “success” function that gets invoked for you, when the HTTP Response is received. As you know from using the browser, the response can arrive quickly, or not so quickly. When you invoke ajax calls in a loop, you’re telling the browser to send out N simultaneous HTTP requests. Not necessarily what you want.

    What I’m suggesting here is to invoke one AJAX call at a time (to the geocoder, for example) and only invoke the next one from the success fn – that is to say, AFTER the response is received from the first one. Also, if you do it using setTimeout(), you will avoid deep fn nesting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ajax call that returns JSON data (Data Attached). After converting the
I have an AJAX call that returns some JSON like this: $(document).ready(function () {
I have an ajax call that has a list of parameters that it needs
I have an AJAX call that I want to run against a WCF GET
I have an Ajax call that updates 5,000 records in a database so this
How can I get request/response logs in selenium? I have an ajax call that
I have an ajax call that returns a HTML fragment. I am trying to
I have an ajax call that gets a list of events for specific users,
I have an ajax call that fires multiple times but is called once. function
I have a jQuery ajax call that returns html of a table. Now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.