
Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[tlug] Variable scope in Javascript driving me nuts
- Date: Fri, 27 Aug 2010 01:29:11 +0900
- From: Dave M G <dave@example.com>
- Subject: [tlug] Variable scope in Javascript driving me nuts
- User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.11) Gecko/20100713 Thunderbird/3.0.6
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