简体   繁体   English

WebSQL和Javascript的操作顺序

[英]WebSQL and Javascript Order of Operation

My application is using the javascript webSQL and I am having some issue with the order of command execution. 我的应用程序使用的是javascript webSQL,并且命令执行顺序出现问题。 No matter what order my code is in the querys get executed last. 无论我的代码在查询中的顺序如何,都将最后执行。 For example in the following code 2 will be alerted before 1: 例如,在以下代码2中,将在1之前发出警报:

db.transaction(
        function (transaction) {
        transaction.executeSql(
        'SELECT * FROM contacts WHERE id = ?;',
        [id],
        function (transaction, result) {
           alert("1");
           if (result.rows.length != 0) {
            user = result.rows.item(0).name;
           } else {}
        },
        errorHandler);
    });

alert("2");
message = id + '%1E' + name;

Any ideas why this is happen? 任何想法为什么会发生这种情况?

When do you alert("2"), you haven't finished the transaction, so the 2nd function you pass to it has not been called. 当您发出alert(“ 2”)时,您尚未完成交易,因此传递给它的第二个函数没有被调用。 Since it's the success handler I assume, it will be called after the transaction has completed successfully. 由于它是我假设的success处理程序,因此将在事务成功完成后调用它。 The third argument would be the code snippet to execute when the query failed, only if it failed. 第三个参数将是查询失败时(仅在查询失败时)执行的代码段。

Anything outside of the event handler code is executed when the page has loaded enough content to execute the javascript. 当页面加载了足以执行javascript的内容时,将执行事件处理程序代码之外的所有内容。 Note that the entire page need not load to execute alert("2") , just enough of the JS. 请注意,整个页面无需加载即可执行alert("2") ,仅够使用JS。 Since these statements are soooo close together, there is bascially 0 chance that the transaction will ever complete before the alert("2") statement is reached and executed. 由于这些语句彼此靠近,因此在到达并执行alert(“ 2”)语句之前,交易完成的可能性基本上为0。

However, if you had enough code between alert("2") and db.transaction(...), it's possible (in what is called a race condition ) that the callback could be executed before the alert(2) code. 但是,如果您在alert("2")和db.transaction(...)之间有足够的代码,则可能(在所谓的竞态条件下 )可以在alert(2)代码之前执行回调。

You want to be careful with event handlers in this case, although it depends on what your success handler does. 在这种情况下,您要小心事件处理程序,尽管这取决于成功处理程序的工作方式。 If it modifies the page DOM, then I would highly recommend wrapping the db.transaction() and surrounding code) in an event handler that is bound to the page loading. 如果它修改了页面DOM,那么我强烈建议将db.transaction()和周围的代码包装在绑定到页面加载的事件处理程序中。

This isn't an answer to your question, but I thought I should give you a warning about webSQL. 这不是您问题的答案,但我想我应该给您关于webSQL的警告。

As of 18 November 2010, W3C had announced that they have deprecated the Web SQL Database recommendation draft and will no longer maintain it. 截至2010年11月18日,W3C已宣布他们已弃用Web SQL数据库建议草案,并将不再维护它。

So while it may WORK in browsers at the moment, I wouldn't rely on it for the future. 因此,尽管目前它可以在浏览器中运行,但将来我将不再依赖它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM