Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tlug] Variable scope in Javascript driving me nuts



TLUG,

Arrgh! This just makes no sense to me. Here's my code:

my function()
{
   var valid = false;
   string = JSON.stringify(data);
   $.post('jsonhandler.php', {JSON: string}, checkResult, "json");
   function checkResult(jsonDataReceived)
   {
      valid = jsonDataReceived.valid;
      console.log ("jsonDataReceived.valid = " + jsonDataReceived.valid);
      console.log ("valid inside checkResult = " + valid);
   }
   console.log ("valid OUTSIDE checkResult = " + valid);
   return valid;
}

When I run this, this is what I see in the console log:

valid OUTSIDE checkResult = false
jsonDataReceived.valid = true
valid inside checkResult = true

Shouldn't what happens inside the checkResult function set "valid" to true before I get to the "return valid" part?

Any advice would be much appreciated.

--
Dave M G



Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links