JavaScript匿名函数变量

时间:2011-12-05 22:29:46

标签: javascript scope

我正在开发一个PhoneGap应用程序,我在循环中有这段代码

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";

我的问题是,tx.executeSql(.... htmlR += li代码

未添加到外部htmlR

1 个答案:

答案 0 :(得分:2)

我怀疑executeSQL不会立即调用回调,而是异步,因此您不会立即看到结果。如果是这样,处理它的正确方法是执行所有依赖于回调中的结果的处理,而不是在调用executeSQL之后。例如,改变:

// Do Something
doSomething();

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

// Do something after SQL
doSomethingElse();

// Do Something
doSomething();

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

    // Do something after SQL
    doSomethingElse();
});