Javascript alert/delay confusion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dsloan
    New Member
    • Nov 2006
    • 1

    Javascript alert/delay confusion

    This problem is very similar to that experienced in http://www.thescripts.com/forum/thread147783.html, but the solution there doesn't seem to apply here.

    I have a function which works fine with the alert call in place below, but a delay (using another function defined elsewhere, which does work by the way) doesn't. Similarly if no delay is put in place it fails to complete the next line. It certainly appears that a value hasn't been finalised at that point, but I'm confused why a delay won't work. If I click the alert box immediately (i.e. a delay of 1-2 seconds) it works.

    Code:
    function loadProperties() {
        new Ajax.Updater('property_div', baseUrl+'/property/ajaxCorrespondenceProperties', {
            method:'post', postBody:'town_id='+$F('town_id')
        });
    
        //alert("setting property selection");
        //pause(5000);
    
        townId = document.getElementById('town_id').value;
        propertyId = document.getElementById('property_id').value;
    }
    Any ideas?
    Diarmid
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    The most likely cause is that the response is not yet ready because the Ajax call is asynchronous. Move the statements to an onComplete event handler or when the readyState is 4 and the status is 200.

    Comment

    Working...