简体   繁体   English

如何从具有phonegap数据库SQLite查询的内部函数返回值到外部函数

[英]how to return a value from inner function that have a phonegap database SQLite query to the outer function

This is my code 这是我的代码

hasSubAcc = function(accPid) {
    $.mobile.eazydb.transaction(function(tx) {
        tx.executeSql('SELECT * FROM Account WHERE Account_Parent_ID="'+accPid+'"', [],
            function(tx, rs){
                if(rs.rows.length == 0) {
                    return false;
                } else {
                    return true;
                }
        });
    });
}

and the method accessed by 以及访问的方法

alert(hasSubAcc(accid));

this alert prints undefined. 此警报打印未定义。 whats wrong with this code. 这段代码有什么问题。 Any solutions plz.Thanks. 任何解决方案,谢谢。

The problem is very clear, as you are using heavy process(ie database) inside a function so jquery runs this function in seperate thread parallel to your main thread now in this condition your value will always get undefined. 问题非常清楚,因为您正在函数中使用繁重的进程(即数据库),因此jquery现在在与您的主线程平行的单独线程中运行此函数,在这种情况下,您的值将始终未定义。

you can make a global variable var hasSubAcc=-1; 您可以使全局变量var hasSubAcc=-1; now after checking condition 现在检查条件之后

if(rs.rows.length == 0) {
                    hasSubAcc=0;
                } else {
                    hasSubAcc=1;
                }

now give a small delay using 现在给使用一个小延迟

setTimeout(function(){
alert(hasSubAcc);

},500);

or

while(hasSubAcc==-1){
//do nothing
}
alert(hasSubAcc);

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

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