ajaxxmlhttprequestonreadystatechange

AJAX - Order of commands with XMLHttpRequest (Newbie)


In most examples I found on the Internet , I see something like this :

ajaxRequest.onreadystatechange = function() {   
    if(ajaxRequest.readyState == 4) {
        document.myForm.time.value = ajaxRequest.responseText;
    }
}

ajaxRequest.open("GET", "serverTime.php", true);
ajaxRequest.send(null);

How is it possible and how does this code work , when the "change state" property is checked BEFORE the open and send commands are executed ? I know it works...but how does the flow return back to "check the state status" after the "open" and "send" are executed.

I would appreciate any help

Many thanks in advance :-)


Solution

  • Because onreadystatechange is an event, and that code won't get invoked until the ready state changes, which will occur at some point in the future when the request is complete.