Question

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");
        });
    }
});
Was it helpful?

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");
        });
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top