Search code examples
javascripthtmlasynchronoushtml5-filesystem

How to know which is the last fileEntry with readEntries?


I am using html5 fileSystem and want to call a function when all entryFiles are fully loaded, but I cannot register any index (file function is asynchronous).

How can I know when it's the last fileEntry?

var dirReader = fileSystem.root.createReader();
dirReader.readEntries(function(fileEntries) {
    for (var i=0;i<fileEntries.length;i++) {
        var myfirstindex = i;
        console.log(myfirstindex); //here prints 0 1 2 3 4 5 before load any file
        fileEntries[i].file(function(file) {
            var mysecondindex = i;
            console.log(mysecondindex); //here is always 5
            //if (index==fileEntries.length-1) console.log("last");
        });
    }
});

Solution

  • Well, I think I did it:

    var dirReader = fileSystem.root.createReader();
    dirReader.readEntries(function(fileEntries) {
        for (var i=0;i<fileEntries.length;i++) {
            if (i==fileEntries.length-1) var last = fileEntries[i].name;
            fileEntries[i].file(function(file) {
                if (file.name==last) console.log("last");
            });
        }
    });