SQLite成功回调在事务完成之前触发

时间:2012-11-26 16:00:50

标签: javascript asp.net sqlite web-sql

在我的ASP.NET Web表单应用程序中,我需要执行一些JQuery操作来收集表单数据,然后遍历表单数据以保存到SQLite数据库,并在成功完成后关闭当前窗口。如果窗口保持打开状态,Save操作将完美地运行,但是一旦我将window.close添加到我的SQLite事务中的成功回调中,窗口似乎在保存操作有时间完成之前关闭。

function save(closeWindow)
{
  //Gathering form data and creating the checkListInsert string containing my bulk insert statement  (i.e. Insert Into Table Select .... Union Select...)

  db.transaction(function (tx) {

                tx.executeSql(checkListInsert.substring(0, checkListInsert.length - 6), [], function (tx, result) {
                    if (closeWindow == "Yes") {
                        //window.close();  If left uncommented the data will not save.  When left commented the save occurs but of course the window is still open
                    }
                }, onSQLError);
        });
}

编辑:如果我将window.clTime()放在window.setTimeout()中1秒钟,一切正常。所以这绝对是一个时间问题。似乎回调不应该在事务完成之前触发?!

2 个答案:

答案 0 :(得分:0)

使用表单中的行数作为预期要插入的行数。比较一下 通过结果rowsAffected来验证交易是否完整:

if (closeWindow == "Yes") 
  {
  if (result.rowsAffected === /*Number of rows inserted*/)
    {
    window.close();
    }
  }

答案 1 :(得分:0)

您应该等待事务完成,而不是executeSql:

function save(closeWindow) {
    db.transaction(function (tx) {
        tx.executeSql(checkListInsert.substring(0, checkListInsert.length - 6), []);
    }, function () { }, function () {
        if (closeWindow == "Yes") {
            window.close();
        }
    });
}
相关问题