这真的是异步的还是它发生得太快以至于没有什么区别?
我正在尝试编写一种在 WebDB 环境中同步执行一组 sqlite 查询的方法。我已经提出了我认为相当于同步例程的内容,但我不确定如何测试它。这就是我所拥有的:
var db = openDatabase("test1", "1.0", "test", 5 * 1024 * 1024);
function synchronousSql(tx, sqlStack, callback) {
if (sqlStack.length !== 0) {
var q = sqlStack.shift();
console.log(+new Date() + ' ' + q);
tx.executeSql(q, [], synchronousSql(tx, sqlStack, callback), null);
} else {
callback();
}
}
var seq = [
'drop table if exists table1',
'drop table if exists table2',
'drop table if exists table3',
'drop table if exists table4',
'drop table if exists table5',
'create table table1(id integer, value text)',
'create table table2(id integer, value text)',
'create table table3(id integer, value text)',
'create table table4(id integer, value text)',
'create table table5(id integer, value text)',
'drop table if exists table1',
'drop table if exists table2',
'drop table if exists table3',
'drop table if exists table4',
'drop table if exists table5'
];
db.transaction(function(tx) {
synchronousSql(tx, seq, function() {
console.log(+new Date() + ' - from synchronousSql callback');
});
}, null, function() {
console.log(+new Date() + ' - from transaction callback');
});
在纸面上(我认为)它应该有效。看着它,我认为我的警报消息会在执行最终语句时弹出,而不一定是在它返回时弹出,但我不知道如何解决这个问题。我尝试在 synchronousSql 函数的参数中指定回调,但这意味着我必须使用回调递归地调用它,并且如果我使用空的匿名函数,它似乎会覆盖所需的回调。
所以我猜测两个问题:第一,这真的是同步的吗?其次,如何实现最终回调以在堆栈的最后一个回调上运行?
编辑:将代码更新到更新版本。不过,第一个问题仍然存在:这真的是同步的吗?
I'm trying to write a way to synchronously execute a set of sqlite queries within a WebDB environment. I've come up with what I think amounts to a synchronous routine, but I'm not sure how to test it. Here's what I have:
var db = openDatabase("test1", "1.0", "test", 5 * 1024 * 1024);
function synchronousSql(tx, sqlStack, callback) {
if (sqlStack.length !== 0) {
var q = sqlStack.shift();
console.log(+new Date() + ' ' + q);
tx.executeSql(q, [], synchronousSql(tx, sqlStack, callback), null);
} else {
callback();
}
}
var seq = [
'drop table if exists table1',
'drop table if exists table2',
'drop table if exists table3',
'drop table if exists table4',
'drop table if exists table5',
'create table table1(id integer, value text)',
'create table table2(id integer, value text)',
'create table table3(id integer, value text)',
'create table table4(id integer, value text)',
'create table table5(id integer, value text)',
'drop table if exists table1',
'drop table if exists table2',
'drop table if exists table3',
'drop table if exists table4',
'drop table if exists table5'
];
db.transaction(function(tx) {
synchronousSql(tx, seq, function() {
console.log(+new Date() + ' - from synchronousSql callback');
});
}, null, function() {
console.log(+new Date() + ' - from transaction callback');
});
On paper (I think) it should work. Looking at it, I think my alert message will pop up when the final statement is executed, not necessarily when it returns, but I'm not sure how to fix that. I tried specifying a callback in my arguments for the synchronousSql
function but that meant I had to call it recursively with a callback, and if I used an empty anonymous function it seemed to overwrite the desired callback.
So I guess two questions: first, is this really synchronous; and second, how can I implement a final callback to run on the last callback of the stack?
edit: updated the code to a more recent version. First question still stands, though: is this truly synchronous?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在代码中添加
alert()
和setTimeout()
来测试它们是同时执行还是分开执行。这通常是一个很好的测试方法。如果所有代码同时执行(异步),那么您将立即隐藏 10 个警报 - 否则每 x 毫秒就会收到一个警报。关于你的第二个问题:只需检查堆栈是否为空。不过,您可能必须通过函数传递第三个变量。除非您在全局范围内定义变量,否则它不会被覆盖。
Put an
alert()
and asetTimeout()
in your code to test whether they all execute at the same time or get separated. That's usually a pretty good way to test it. If all code gets executed at the same time (async) then you'll instantly have 10 alerts to hide - else you get one per x ms.On your second question: just check whether the stack is empty. You'll probably have to pass a third variable through your function though. Unless you define the variable on the global scope, it won't get overwritten.