1

I'm working on a PhoneGap app and I have this piece of code in a loop

htmlR += "html code here";
tx.executeSql('SELECT Question,Grp from KnowSelf where Dimension = "'+result.Dimension+'"', [], function(tx,resultR){

    var leng = resultR.rows.length;
    for(var i = 0; i < leng; i++){
        var resultsR = resultR.rows.item(i);
        htmlR += '<li class="catsli">'+resultsR.Question+'</li>';
        htmlR += '<li class="line"><img class="line" src="iPhone3/Line.png" alt="line"/></li>';

    }

},errorCB);
htmlR += "continue html code here";

My problem is, inside the tx.executeSql(.... htmlR += li tags

is not adding to the outer htmlR.

4
  • 1
    It should be, the function you're passing into executeSql is a closure over it and I don't see it being shadowed anywhere. Are you sure the callback accepted by executeSql happens synchronously? Sounds like it may be async, hence your not seeing the results immediately. Commented Dec 5, 2011 at 22:31
  • @T.J.Crowder: I strongly assume that it's async; otherwise, it wouldn't need a callback. Commented Dec 5, 2011 at 22:32
  • @SLaks: I expect so, but it's not the only reason for one. :-) Commented Dec 5, 2011 at 22:33
  • You guys are right, it is async. Anyway around this? Commented Dec 5, 2011 at 22:34

1 Answer 1

3

I suspect that executeSQL doesn't call the callback immediately, but rather asynchronously, and so you don't see the results immediately. If so, the correct way to handle it is to do all of your processing that relies on the results from within the callback, rather than after calling executeSQL. E.g., change:

// Do Something
doSomething();

// Do some SQL
executeSQL(..., function() {
   // ...deal with callback...
});

// Do something after SQL
doSomethingElse();

to

// Do Something
doSomething();

// Do some SQL
executeSQL(..., function() {
   // ...deal with callback...

    // Do something after SQL
    doSomethingElse();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Although you're solution made sense, it does not work in my case. But thanks for the async tip, i will just change my logic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.