Google Chrome、WebWorks、HTML5Database、transaction() 和警报 - 未调用事务回调
我们遇到了一个奇怪的情况。我们正在调用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 thetxCallback
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑这个错误是一个时间问题。
数据库事务是异步的,而警报语句是同步的(阻止 UI),因此在执行警报语句时不能保证事务调用已完成:
建议完全删除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:
Suggest removing alert() statements completely.