0

我们遇到了一个奇怪的情况。我们正在调用 a database.transaction(txCallback, txError, txSuccess),如果我们在事务调用之后调用一个alert()调用,txSuccess则调用该函数而不会调用该 txCallback函数。

这是一个已知错误,还是有合理解释的记录行为?

它似乎只出现在 Ripple Emulator 和 Google Chrome(Ripple 所基于)中。它不会出现在 Safari 中,无论是使用alert还是console.log.

这个 HTML 很好地说明了这种情况:

<html>
<head>
<script>
function dbalert() {
  var db = window.openDatabase("test","1.0","test",1024*1024);
  console.log("Next line should read: In transaction callback");
  window.transactionCalled = false;
  db.transaction(
    function (tx) {
        console.log("In transaction callback");
        window.transactionCalled = true;
    },
    function (tx, err) {
        console.error("ERROR");
        console.log(err);
    },
    function () {
        if (window.transactionCalled) {
            console.log("Success callback: everything worked!");
        } else {
            console.error("Success callback: BUT TRANSACTION WAS NEVER CALLED");
        }
    }
  );
  /*****
   * Change to FALSE to get this working.
   *****/
  if (true) {
    alert("Ok, let's see what happened");
  } else {
    console.log("Ok, let's see what happened");
  }
}
</script>
</head>
<body onLoad="dbalert();">
<div id="out">
</div>
</body>
</html>
4

1 回答 1

0

我怀疑这个错误是一个时间问题。

数据库事务是异步的,而警报语句是同步的(阻塞 UI),因此在执行警报语句时不能保证事务调用已经完成:

如果我们在事务调用之后调用 alert()

建议完全删除 alert() 语句。

于 2012-11-02T13:02:54.230 回答