Question

In the function "Encaisser", the value of "i" is OK in the for, but if i call 'i' in a function in my function, "i" return "Undefined.

function Encaisser()
{

   for(var i=1; i <= Nombre_ligne_en_caisse; i++)
   {    
      db.transaction(function(t,i){ t.executeSql('SELECT En_cour FROM Sequence WHERE Nom="Ticket_ID"', [], function(tx,rs,i){ 

         var row = rs.rows.item(0); 
         var Tick_ID = row['En_Cour']; 
         var Noma = window['Produit_en_caisse_' + i]  ;

         alert(i); //Undefined
         alert(Noma); //Undefined

      }, [])});
   alert(i); //If i put the alert here, its OK
   }

}

Do you know why?

Thank You,

Was it helpful?

Solution

The problem is that your inner function defines a parameter named i here:

db.transaction(function(t,i){ ...

If you intend for i to be the value from the outer function, I recommend you simply remove this parameter. It doesn't appear that db.transaction is actually providing a value for this parameter anyway. You'll probably also want to close the value of i at each iteration in a separate variable, and use that inside your function, like this:

var index = i;
db.transaction(function(t){ ...

     var Noma = window['Produit_en_caisse_' + index ];

     alert(index);

OTHER TIPS

You redefine i inside both your db.transaction callback and your t.executeSql callback. Inside your t.executeSql callback, i must be undefined.

If you want to access the value of i from the for loop, you'll need to rename those parameters in your callbacks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top