Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Phonegap读取JavaScript中的文件_Javascript_File_Cordova_Closures - Fatal编程技术网

使用Phonegap读取JavaScript中的文件

使用Phonegap读取JavaScript中的文件,javascript,file,cordova,closures,Javascript,File,Cordova,Closures,我有一个对象表(x,y,z,…),每个表包含一个文件路径和一个id。还有它们自己的方法:getPath()和getId() 因此,我希望读取每个文件并将数据存储在id指向的html元素中 但当我这么做的时候: function readFile(path, callback, context) { var reader = new FileReader(); r.readAsText(path); // here if I display the objects, I've got in

我有一个对象表(x,y,z,…),每个表包含一个文件路径和一个id。还有它们自己的方法:
getPath()
getId()

因此,我希望读取每个文件并将数据存储在id指向的html元素中

但当我这么做的时候:

function readFile(path, callback, context)
{
 var reader = new FileReader();
 r.readAsText(path);
 // here if I display the objects, I've got in order x,y,z,....
 r.onloadend = function ()
               {
                  // Here is the problem
                  // If x and y has the same "path" attribute value
                  // I've got in order : y,y,z ....
                  callback(r.result, context); 
               } 
}

function main(tabObject)
{
 for (var i = -1; ++i < tabObject.length;)
  {
     readFile(tabObject[i].getPath(),
              function (result, context)
               {
                  // Doing stuff with the result
                  console.log("the id of the html tag is : " + context.getId());
                  console.log("this is what I read" + result);
               },
               tabObject[i]);

  }
}
函数readFile(路径、回调、上下文)
{
var reader=new FileReader();
r、 readAsText(路径);
//在这里,如果我显示对象,我得到的顺序是x,y,z,。。。。
r、 onloadend=函数()
{
//问题就在这里
//如果x和y具有相同的“路径”属性值
//我有顺序:y,y,z。。。。
回调(r.result,context);
} 
}
函数main(tabObject)
{
对于(var i=-1;++i
如果所有文件路径都不相同,则该选项有效。但例如:

如果前两个对象(“x”和“y”)具有相同的path值(如
C://Directory/stuff.png

r.onloadend
函数将使用同一对象调用两次(使用
y
调用两次)。我认为这是因为对于二次读取(对于'y'),函数不需要更多的时间来读取文件(已经用'x'读取),因此在“调用堆栈”中,
y
将擦除
x
(函数的关闭可能不太好)

我希望有人能告诉我发生了什么,以及如何解决


我找到了一个“解决方案”,实际上我使用“setInterval”函数将每个读取时间间隔为100毫秒。好的,这是可行的,但这个解决方案非常丑陋,所以我让这篇文章,直到有人能告诉我发生了什么

你有没有试过在文件名的末尾放一个缓存buster字符串,比如“c://Directory/stuff.png?123123”——只是一个随机数。我试过放一个缓存buster字符串,但它也不起作用