0

HELP! I am having an issue w/ one of my Javascript functions that has an inner Javascript function driven by an 'onreadystatechange'.

So all I am looking to do in my main JS code is figure out the value of jsonWR.SURG.EXIST_IND when this function runs. When the INNERretval alert runs, the value is correctly outputting to 1....this is what I expect. Then right before I return the value at the end of the main surgeryScheduled() function, ENDretval is saying the value is now 0.

I am sure I am missing something fundamental here, so can someone help me.

Plus, when surgeryScheduled() function is being called, I set a variable to the return value:

var q = 0;
q=surgeryScheduled(<someID>);
alert("q:"+q);

The above alert returns 1 when I would expect it to return 0 since ENDretval above was 0. What am I missing here??

function surgeryScheduled(srgnID){

 var srgnPrsnlID = srgnID;                 
 var sdt         = document.getElementById('sdt').value;
 var sdtc        = sdt.toUpperCase();
 var edt         = document.getElementById('edt').value;
 var edtc        = edt.toUpperCase();

 var paramString;
     paramString =""
     paramString +=            '^MINE^,'
                  +             srgnPrsnlID
                  +             ',^'
                  +             sdtc
                  +             '^,^'
                  +             edtc
                  +             '^'

 var WRInfo         = new XMLCclRequest();

 //;  Call the ccl progam and send the parameter string
 WRInfo.open("GET", "CH_MP_HAVE_SURGERY_SCHED");
 WRInfo.send(paramString);

 //; Get the response
 WRInfo.onreadystatechange = function () {
    if (WRInfo.readyState == 4 && WRInfo.status == 200) {
       var jsonWRResp = WRInfo.responseText;

       if (jsonWRResp != undefined && jsonWRResp.length !=0) {
          jsonWR = eval('(' + jsonWRResp + ')');
       }

      //; Make sure everything checks out, then return bool
      if (jsonWR){
          retval    = jsonWR.SURG.EXIST_IND;   
          alert("INNERretval:"+retval);

      }//;jsonWR

    }//;WRInfo.readyState == 4 && WRInfo.status == 200
}//;WRInfo.onreadystatechange 

alert("ENDRetval:"+retval);
return retval;

}

Will
  • 1
  • 4
  • This is probably because by the time you clear the ENDRetval alert the ajax request has completed and the readystatechange function has had time to set `retval` before the next alert is called. Remove the ENDRetval alert and test – Patrick Evans Jan 14 '15 at 15:32
  • Well the ENDRetval alert is outside of the AJAX call though...would that still have any effect? The additional issue is that my outside variable that I am setting to be the result of surgeryScheduled() running, is being set to 1....and I am just not sure if that is just because surgeryScheduled() ran to completion or what I just have the ENDRetval alert to try to see what value my function is returning. – Will Jan 14 '15 at 15:40

0 Answers0