Webkit executesql clause and loop problem

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
{
//do something
}
,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
{
//do something
})(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
    {
    //do something
    })(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.

0
source share
1

:

for (var i = 0; i < count; i++) {
   tx.executeSql('SELECT AColumn FROM ATable WHERE refID=' + i, [],
      (function(i){
         return function(tx,results) //success function
         {
            //do something
         };
      })(i),
   errorfunction);
}

function(tx,res), , , (function(i){ return function(tx,res){ ... }; })(i), .

i i (.. ), i , i .

+2

All Articles