I am trying to use a loop variable in the executeSql function that is contained in a loop. But the loop variable gets the last value if I don't use closure. When I use closure, I do not get a list of results from the executeSql function. Examples:
for (var i = 0; i < count; i++) {
tx.executeSql('SELECT AColumn FROM ATable WHERE refID=' + i, [],
function(tx,results) //success function
{
}
,errorfunction);
}
The success function "i" is always "count + 1".
To solve this problem, I changed my code as follows:
for (var i = 0; i < count; i++) {
tx.executeSql('SELECT AColumn FROM ATable WHERE refID=' + i, [],
(function(tx,results) //success function
{
})(i)
,errorfunction);
}
In doing so, I get "i" correctly. But the "results" are undefined.
I tried passing "tx" and "i" as follows:
(function(tx,results) //success function
{
})(tx,null,i)
With this, I understand why I get the "results" as null. I want to know how I can get the correct executeSql results.
source
share