Javascript中window.requestFileSystem的数组作用域



下面的代码给出了一个冲突的答案。数组是全局的,但不在函数范围内?我不明白。这是代码

  var pictures = new Array();
  var app = {
  initialize: function() {
    this.bindEvents();
  },
  bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);       
  },
  onDeviceReady: function() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
        function(fileSystem) {
            fileSystem.root.getDirectory("DCIM/Camera/", {create: false},
                function(dataDir) {
                   var directoryReader = dataDir.createReader();
                   directoryReader.readEntries(
                       function(entries){
                           var i;                           
                           for (i=0; i<entries.length; i++) {
                               pictures[i] = entries[i].fullPath;                                   
                            }
                            console.log(pictures.length + ' ---- in');
                        }, fail)
                }, fail);
        }, fail);  
    console.log(pictures.length + ' ---- out');

当图片进入功能时,webconsole值=176

当图片显示功能时,网络控制台值=0

为什么?

提前感谢

这里的问题是FileSystem API调用是异步的。

例如,如果您运行代码:

var i = 0;
setTimeout(function() {
  i = 10;
  console.log('i in: ' + i);
}, 100);
console.log('i out: ' + i);

您将看到我的行为与您所看到的类似,因为setTimeout发生在"console.log('iout:'+I);"之后。同样的概念也适用于您正在使用的FileSystem调用。

最新更新