简体   繁体   English

异步Javascript

[英]Asynchronous Javascript

function buildRpt() {
  /* Initialize document here */
  db.transaction(
    function(trn) {
      trn.executeSql(
        'SELECT * FROM table',
        null,
        function(trn,result) {
          for (var iI = 0; iI < result.rows.length; iI++) {
            var row = result.rows.item(iI);

            /* Add record data to document here */
            trn.executeSql(
              'SELECT * FROM detail table WHERE id = ?',
              [ row.id ],
              function (trn,rslt) {
                /* Add detail info to document */
                for (var iTmp = 0; iTmp < rslt.rows.length; iTmp++) {
                  var tmpRow = rslt.rows.item(iTmp);
                  /* update document */
                }
              },
              errorHandler
            );
          }
        },
        errorHandler
      );
    }
  );
}

I need to get information from the client side database and use it to populate a document. 我需要从客户端数据库中获取信息,并使用它来填充文档。 I can iterate through the records of a table and I'm fine. 我可以遍历表的记录,我很好。 However, when I try to do another database query to get detail information for each record, javascript does the inner query asynchronously. 但是,当我尝试执行另一个数据库查询以获取每个记录的详细信息时,javascript会异步执行内部查询。 I looked at other questions but I am still kind of foggy on how to accomplish this task. 我看了其他问题,但对于如何完成此任务我还是有些困惑。 In the code above, the nested executeSql is processed asynchronously. 在上面的代码中,嵌套的executeSql是异步处理的。 The detail information never ends up in the document. 详细信息永远不会出现在文档中。 Is there a way to get the detail information for each record into the document where it belongs? 有没有一种方法可以将每个记录的详细信息获取到它所属的文档中?

I'm not sure what the question is? 我不确定问题是什么? If the problem is that nothing comes back use console.log to see if data is coming out. 如果问题是什么都没有回来,请使用console.log查看是否有数据出来。 The issue is likely due to your reference to the DOM that is being updated. 该问题可能是由于您引用了正在更新的DOM。 If the issue is getting requests in a particular order read below. 如果问题是按特定顺序获取请求,请阅读以下内容。

Deferred objects 递延对象

It would be nice to see if you have a framework available to you. 很高兴看到您是否拥有可用的框架。 Since you are issuing AJAX requests in a loop, you need something to synchronize those requests as you will not know in what order they will be returned. 由于您是在循环中发出AJAX请求,因此您需要一些东西来同步那些请求,因为您不知道它们将以什么顺序返回。 For example, 例如,

 requestRows ->

   rows.each ->

     ajax call -> //These return in any order

If you insist on doing this framework free look into deferred objects, and be prepared to do a lot of work. 如果您坚持要执行此框架,请随意查看延迟的对象,并准备做很多工作。 Otherwise if you use dojo use http://dojotoolkit.org/reference-guide/dojo/Deferred.html or jQuery I have written something that does this: https://gist.github.com/1219564 否则,如果您使用dojo,请使用http://dojotoolkit.org/reference-guide/dojo/Deferred.html或jQuery,我已编写了以下代码: https : //gist.github.com/1219564

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

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