function f() {
db.transaction(function(tx) {
tx.executeSql('SELECT name FROM HeadersTable', [], resultsHndlr);
document.writeln('3: {'+aGlobalVariable+'}<BR>');
});
}
... and here's the output in Chrome 7.0.517.44:
3: {GLOBAL}
1: {GLOBAL}
2: {LOCAL}
As you can see, global variables are indeed visible from resultsHndlr(), but any attempt to modify them, even fully qualified, is kept locally.
Would greatly appreciate any help, or suggestions around this. I desperately need a '3:{LOCAL}'. Thanks in advance.
Hmmmm... I believe the output there is correct. Let's break it down a bit, the order in which things are happening is the key issue here.
1. First function executed is f() which also invokes tx.executeSql and passes to it the callback function "resultsHndlr", which will actually be invoked when tx.executeSql is finished I presume. So function f actually makes it to the next line of it's code before resultsHndlr is invoked (this may or may not always be the case, unsure). Thus 3: {GLOBAL} is the first output, and correctly so, as aGlobalVariable has not been altered yet.
2. After that, then resultsHndlr is invoked within tx.executeSql and outputs 1: {GLOBAL} then changes the value of aGlobalVariable to 'LOCAL' and outputs 2: {LOCAL]
And so it would seem to be explained.
Last edited by astupidname; 12-07-2010 at 02:37 AM.
Hi,
Thanks for your reply, that is exactly what is happening, callback function executes after main function (f()) returns, but I don't know exactly when, so in practical terms, I can't modify a global javascript variable when I need to.
It's like MS-windows: I need a SendMessage() function, but I only got a PostMessage(), i.e., to modify one lousy global variable, I need a swarm of messages and code. There should be a way around this, shouldn't it?
Regards.
Bookmarks