Google Chrome、WebWorks、HTML5Database、transaction() 和警报 - 未调用事务回调

发布于 2024-12-28 23:40:27 字数 1419 浏览 7 评论 0原文

我们遇到了一个奇怪的情况。我们正在调用database.transaction(txCallback, txError, txSuccess), 如果我们在事务调用之后进行 alert() 调用,则将调用 txSuccess 函数,而无需 txCallback 函数被调用。

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

它似乎只发生在 Ripple 模拟器和 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>

We've run into a strange situation. We're calling a database.transaction(txCallback, txError, txSuccess),
and if we follow the transaction call with an alert() call, the txSuccess function is called without the
txCallback function ever being called.

Is this a known error, or documented behaviour with a reasonable explanation?

It seems only to occur in the Ripple Emulator and in Google Chrome (which Ripple is based on). It does not occur in Safari, where it operates as expected, whether using alert or console.log.

This HTML demonstrates the situation well:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡月浅 2025-01-04 23:40:27

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

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

<块引用>

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

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

I suspect the bug is a timing issue.

Database transactions are asyncronous, while alert statements are synchronous (block the UI) so the transaction call is not guaranteed to have finished when the alert statement is executed:

if we follow the transaction call with an alert() call

Suggest removing alert() statements completely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文