通过函数附加全局数组显示为长度为零别处 [英] Global array appended by a function shows up as zero length elsewhere

查看:119
本文介绍了通过函数附加全局数组显示为长度为零别处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经声明数组在我的脚本的顶部标签了。

I have declared the array labels up at the top of my script

var labels = [];

有一个功能,retrieveLabels被称为追加到该数组:

A function, retrieveLabels is called to append to this array:

     function retrieveLabels() {
      labels = [];
      var getLabelsQuery = "SELECT DISTINCT label FROM items ORDER BY label;"
      db.transaction(function(tx) {
           tx.executeSql(getLabelsQuery, [],
                function(tx, labelsResults) {
                     for (var x = 0; x < labelsResults.rows.length; x++) {
                          var labelsRow = labelsResults.rows.item(x);
                          labels.push(labelsRow['label']);
                     }
                     console.log(labels.length);
                     console.log(labels);
                }
           );
      });
 }

第一个控制台消息显示34项。
第二个控制台消息显示[LABEL1,LABEL2.....label34]

The first console message shows 34 items. The second console message shows ["label1", "label2"....."label34"]

我在哪里调用该函数是在这里:

Where I call the function is here:

else {
 init_db();
 retrieveLabels();
 console.log(labels.length);
 console.log(labels);

}

第一个控制台消息显示0项。
第二个控制台消息显示[LABEL1,LABEL2.....label34]

The first console message shows 0 items. The second console message shows ["label1", "label2"....."label34"]

为什么会这样突然成了0长度的数组?或者是它同时修改了retrieveLabels内?

Why would this all of a sudden become a 0 length array? Or was it modified while inside of retrieveLabels?

推荐答案

我要说的是, tx.executeSql()是异步的,并通过了匿名函数的参数3是以后实际执行的 retrieveLabels();返回

I would say that tx.executeSql() is asynchronous, and the anonymous functions passed as argument3 is actually executed after retrieveLabels(); has returned.

所以当时你的第二个的console.log(labels.length); 被调用,查询可能不会被执行

So at the time your second console.log(labels.length); is called, the query may not have been executed.

下面是需要一个回调作为参数,并调用它一旦标签数组已被填充你的函数略加修改的版本:

Here is a slightly modified version of your function that takes a callback as argument, and calls it once the labels array has been populated:

function retrieveLabels(callback) {
    var labels = [];
    var getLabelsQuery = "SELECT DISTINCT label FROM items ORDER BY label;"
    db.transaction(function(tx) {
        tx.executeSql(getLabelsQuery, [],
            function(tx, labelsResults) {
                for (var x = 0; x < labelsResults.rows.length; x++) {
                    var labelsRow = labelsResults.rows.item(x);
                    labels.push(labelsRow['label']);
                }               

                callback(labels);
            }           
        );      
    }); 
}

然后就可以调用该函数是这样的:

Then you can call the function like this:

init_db();
retrieveLabels(function(label) {
    console.log(labels.length);
    console.log(labels);
});

这篇关于通过函数附加全局数组显示为长度为零别处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆