1

can you please suggest me why the value of i increase by own when i am fetching data from data base . Actually i am fetching data from table in phone gap .But along with i also count the number of elements in another table .while fetching the value of i increase by own and getting an error..Item index is out of range.

function getallTableData(tx) {

    tx.executeSql('SELECT * FROM CaseTable', [], querySuccess, errorCB);
}

function querySuccess(tx, result) {

    var len = result.rows.length;
    var countDoument = 0
    $('#folderData').empty();
    for (var i = 0; i < len; i++) {

        alert(i) // here value is i =0

        test1 = result.rows.item(i).CaseName;



        Test1(test1, function (result_count) {
            countDoument = result_count; // here it count value 
            alert(result_count + "result_count") //alert is correct count value
            alert(i + "i"); //here i comes 1 why ? it should be 0

            $('#folderData').append(
                '<li class="caseRowClick" id="' + result.rows.item(i).id + '" data-rel="popup" data-position-to="window">' + '<a href="#">' + '<img src="img/Blue-Folder.png">' + '<h2>' + result.rows.item(i).CaseName + countDoument + '</h2>' + '<p>' + result.rows.item(i).TextArea + '</p>' + '<p>' + result.rows.item(i).CaseDate + '</p>' + '</a>' +
                '<span class="ctrl togg"><fieldset data-role="controlgroup" data-type="horizontal" data-mini="true" ><button class="edit button_design">Edit</button><button class="del button_design">Delete</button></fieldset><span>' + '</li>'
            );

        });

        $('#folderData').listview('refresh');




    }


}

function Test1(test, callBack) {
    var x;
    db.transaction(function (tx) {
        $yoursql = 'SELECT * FROM  "' + test + '" ';
        tx.executeSql($yoursql, [], function (tx, results) {
            x = results.rows.length + "TableName" + test;
            callBack(x);
        });
    });

}

Check my comments also..!!

Pointy
  • 405,095
  • 59
  • 585
  • 614
Ravi Aaaaaa
  • 216
  • 5
  • 12

1 Answers1

1

The callback you pass through "Test1" to the database function is not called immediately. It's called later, when the database operation completes. By the time that happens, "i" has already been incremented.

If you want to preserve the value of "i" as it was when the database operation was started, you have to make a copy of it. The way to do that in JavaScript is to wrap your anonymous function in another anonymous function, like this:

    Test1(test1, function(i) {
      return function(result_count) {
        countDoument = result_count; // here it count value 
        alert(result_count + "result_count") //alert is correct count value
        alert(i + "i"); 

        $('#folderData').append(
            '<li class="caseRowClick" id="' + result.rows.item(i).id + '" data-rel="popup" data-position-to="window">' + '<a href="#">' + '<img src="img/Blue-Folder.png">' + '<h2>' + result.rows.item(i).CaseName + countDoument + '</h2>' + '<p>' + result.rows.item(i).TextArea + '</p>' + '<p>' + result.rows.item(i).CaseDate + '</p>' + '</a>' +
            '<span class="ctrl togg"><fieldset data-role="controlgroup" data-type="horizontal" data-mini="true" ><button class="edit button_design">Edit</button><button class="del button_design">Delete</button></fieldset><span>' + '</li>'
        );
      };

    }(i));

Note that on the last line, the wrapper function is invoked and passed the value of "i". The wrapper function returns your original function, so "Test1" is still passed the same thing. The difference is that now your original function is built in the wrapper, and the wrapper has its own private copy of "i". When the database transaction finishes and your function is called, then that copy of "i" will still have the value it had when the operation started.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • That is the awesome answer ..!! From morning i struck here in this problem..!! – Ravi Aaaaaa Jul 20 '13 at 14:41
  • Like print 1 to 10 number along with some asyn task..!! so i can understand well – Ravi Aaaaaa Jul 20 '13 at 14:47
  • @RaviAaaaaa there are lots and lots of Stackoverflow questions very similar to this. It's a really common problem in JavaScript programming. Examples [here](http://stackoverflow.com/questions/341723), [here](http://stackoverflow.com/questions/6487366), [here](http://stackoverflow.com/questions/7268579). There are many different situations, but the basic problem is the same. – Pointy Jul 20 '13 at 15:07