jquery - 为什么执行N次的事务函数中变量只有最后一个返回值?

标签 jquery sqlite cordova

我正在开发一个phonegap应用程序,它将使用sqlite数据库。该数据库的唯一更新将涉及其内容而不是其方案。

保持本地sqlite数据库更新的方法是:

  1. 有一个远程数据库,其中的表有两列:“id”和“sql_sentence”。它存储要在本地执行的一组sql_sentences,以便更新本地sqlite 数据库。

  2. 在本地,我有一个 var 'local_max_id',它保存本地执行的最后一个 sql_sentence 的 id。应用程序第一次运行时其值为 0。

  3. 每次应用启动时,它都会将“local_max_id”的值与远程数据库“id”列中的最高值进行比较。

  4. 如果值匹配,则本地数据库是最新的。否则,我必须检索 id 大于“local_max_id”的 sql_sentences 集合,执行它并将“local_max_id”更新为最后收到的行 id。

执行此操作的代码(有一些西类牙语消息,但我尝试用英语提供必要的信息):

var min_id;
var local_max_id=0;
var sentencia_sql   = "";

function initDB(){
        //Just to make sure it requests the database info. Out when in production 
        window.localStorage.setItem("local_max_id", 0);

        // Used to see which has been the last sql_sentence id executed locally  
        local_max_id    = window.localStorage.getItem("local_max_id");

        //Call to retrieve the highest id in remote database
        $.post("http://mytests.es/phonegap-db/get-db.php", {exec:"max_id"}, treat_max_id, "json");
}


//Method that decides what to do depending on returned remote_max_id value and local_max_id.
function treat_max_id(remote_max_id){
    if(remote_max_id > local_max_id){
        $.post("http://mytests.es/phonegap-db/get-db.php", {exec:"get_sentencias", "min_id":local_max_id}, 
            function(res) {
                if(res){
                    for(j=0;j<res.length;j++){
                        sentencia_sql   = res[j].sentencia;
                        alert(sentencia_sql);
                        //The problem is here. While the alert shows the proper sql_sentence of each iteration, this variable (sentencia_sql) only has the last returned value inside transaction function, but is executed N times (being N the amount of returned sql_sentences)
                        transaction(
                            function(tx){
                                console.log(sentencia_sql);
                                tx.executeSql(sentencia_sql);   
                            },
                            errorDB,
                            succesDB2
                        );
                    }
                    window.localStorage.setItem("local_max_id", remote_max_id);
                }
                else{
                    console.error("No se ha recuperado resultado de get-db.php/get_sentencias");
                }
            },"json"
        );
    }
}

用要执行的 N 个 sql 语句处理结果的循环的行为方式对我来说很难理解。尽管应该在本地执行 sql 语句的方法“transaction”位于循环内部并且每个接收到的行执行一次,但变量“sentencia_sql”始终具有相同的值,即最后接收到的 sql 语句。

正常吗?我应该使用任何指令来控制这种异步行为吗?

最佳答案

异步 ​​JavaScript 需要一些时间来适应 - 代码不会按照编写的顺序运行,并且循环的所有迭代共享相同的范围。

Adding rows to a sqlite database using a loop (Phonegap)

关于jquery - 为什么执行N次的事务函数中变量只有最后一个返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13777824/

相关文章:

javascript - 页面加载时默认隐藏所有div,显示:none doesn't appear to work

iphone - 如何提高 SQLite3 iPhone 程序的性能

android - PickContact 需要 android.permission.READ_CONTACTS,或者 grantUriPermission()

android - 使用媒体插件播放mp3的Phonegap应用程序不起作用

java - 获取两个日期之间包含 'date_added' 的行不起作用

javascript - 如何使用查询结果 - Javascript(Azure 数据库)

javascript - 使用 jQuery sortable 交换两个 div

jquery - 切换下一个元素 (jQuery)

javascript - 如何在 JavaScript 中触发窗口调整大小事件?

sqlite - 在 qt 中使用 sqlite 数据库