I'm trying to use loop variable in executeSql function that contained by loop. But loop variable gets the last value if i don't use a closure. When i use a closure , i don't get the result list from executeSql function. Examples:
for (var i = 0; i < count; i++) {
tx.executeSql('SELECT AColumn FROM ATable WHERE refID=' + i, [],
function(tx,results) //success function
{
//do something
}
,errorfunction);
}
In success function "i" is always "count+1".
To solve this i changed my code like this:
for (var i = 0; i < count; i++) {
tx.executeSql('SELECT AColumn FROM ATable WHERE refID=' + i, [],
(function(tx,results) //success function
{
//do something
})(i)
,errorfunction);
}
With this, i get the "i" right. But "results" is undefined.
I tried to pass "tx" and "i" like this:
(function(tx,results) //success function
{
//do something
})(tx,null,i)
With this i understand why i get "results" as null. I want to learn how can i get the right results of executeSql.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…