是文件系统在javascript中的不同线程上运行

时间:2015-01-23 06:56:15

标签: javascript multithreading html5-filesystem

我使用了javascript的文件系统,我注意到文件系统在执行读取后创建了文件,但是在调用readFile之后实际调用了writeFile

readFile

var readFile = function () {
console.log("readfile" + new Date());
var that = this;
var result = "";
var successCallback = function (fs) {
console.log("Inside readFile()");
fs.root.getFile('rosters.txt', {create: false, exclusive: false},
  function (fileEntry) {
    fileEntry.file(function (file) {
      var reader = new FileReader();
      reader.onloadend = function(e) {
        console.log(this.result);
        result = this.result;
        that.showRosters(this.result);
      }; //onloadend() ENDS HERE
      reader.readAsText(file);
    },  //fileEntry success callback ENDS HERE
    function () { console.log("Error occurred during reading file");
    result = "Error";  } //error callback for fileEntry.file
    );  //fileEntry.file ENDS HERE  
  }, //fs.roor.getFile success callback ENDS HERE
  function () { console.log("File Not Found"); } ); //error callback fs.root
 }
 var errorCallback = function (e) {
 console.log(e.toString());
 }
 this.EMarshal.reqFs.call(window,this.EMarshal.persistent, 5*1024*1024 /*5MB*/,       successCallback, errorCallback);
return result
}

writeFile

var writefile = function (rosters) {
console.log("writeFile" + new Date());
var that = this;
console.log("Rosters" + rosters);
var successCallback = function (fs) {
console.log('Opened file system: ' + fs.name);
fs.root.getFile('rosters.txt', {create: true, exclusive: false},
  function(fileEntry) {
    console.log("File Created");
    fileEntry.createWriter( function(writer) { 
      that.gotFileWriter(writer,rosters);
      console.log("write");
      }
      ,function() { console.log("Failed"); 
        this.EMarshal.fail() }
    );
  },
  function (e) {
    console.log(e.toString()); 
  } );
}
var errorCallback = function (e) {
   console.log(e.toString);
};
console.log('Error: ' + msg);
}
this.EMarshal.reqFs.call(window,this.EMarshal.persistent, 5*1024*1024 /*5MB*/, successCallback, errorCallback);

}

代表gotFileWriter

 var gotFileWriter = function (writer,roster) {
 writer.seek(writer.length);
 writer.truncate(0);
 roster = "tripid:" + this.tripid + roster;
 writer.write(roster);
}

我试着抽出时间,然后readFile也在writeFile之前运行。 这里有javascript运行2个主题吗? 如果有人能够解释为什么会发生这种情况会有所帮助。

1 个答案:

答案 0 :(得分:1)

尝试检查name中的js-module属性。我在使用相同插件时遇到此问题,它应该与插件名称相同。

如果无效,请切换到其他PhoneGap-SMS插件。这将很有用。

如果您将阅读有关cordova插件的文档,您将了解如何构建这些插件。 Cordova Plugin Documentation