I have the following code:
var ids = new Array(5);
$(function(){
html5sql.process(sql,
function(transaction, results, rowsArray){
for(var i = 0; i < rowsArray.length; i++){
ids[i] = rowsArray[i].ID;
console.log(ids[i]); //returns the actual value for i
}
},
function(error, statement){
console.log("Error: " + error.message);
}
);
console.log(ids[0]); //returns undefined
});
As you can see, the print out inside the for loop prints the correct value. On the other hand, when I print outside the function I get undefined. What is the problem with the scoping? How can I get the actual value of the array outside the function?
Thanks
html5sql.process()runs asynchronously. The call toconsole.log()occurs immediately afterward and before any values are assigned to ids. You need to delay the call to console.log until processing is finished (and values have been assigned). I.e. you have a timing issue, not a scoping issue.