简体   繁体   English

JavaScript匿名函数变量

[英]JavaScript anonymous function variable

I'm working on a PhoneGap app and I have this piece of code in a loop 我正在开发一个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";

My problem is, inside the tx.executeSql(.... htmlR += li tags 我的问题是,在tx.executeSql(.... htmlR += li标签内)

is not adding to the outer htmlR . 没有添加到外部htmlR

I suspect that executeSQL doesn't call the callback immediately, but rather asynchronously , and so you don't see the results immediately. 我怀疑executeSQL不会立即调用回调,而是异步调用,因此您不会立即看到结果。 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 . 如果是这样,处理它的正确方法是执行所有依赖于回调内部结果的处理,而不是在调用executeSQL Eg, 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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM