Chrome应用程序:无法检索文件加载状态
Chrome App: Cannot retrieve file load status
我的Chrome应用程序有一个函数,它要求另一个函数加载一个文件,检查该函数是否设置了表示成功的标志(External.curFile.lodd
),然后尝试处理它。我的问题是,第一次调用该函数时没有设置标志,但第二次调用时已经设置了标志。
我有一种感觉,这与Chrome文件函数是异步的有关,所以我在加载文件时让第一个函数空闲了一段时间。不管我等多久,第一次加载都不会成功,但第二次加载总是成功!
调用功能:
function load_by_lines_from_cur_dir( fileName, context ){ // determine the 'meaning' of a file line by line, return last 'meaning', otherwise 'null'
var curLineMeaning = null;
var lastLineValid = true;
External.read_file_in_load_path(fileName); // 'External' load 'fileName' and reads lines, REPLacement does not see this file
// This is a dirty workaround that accounts for the fact that 'DirectoryEntry.getFile' is asynchronous, thus pre-parsing checks fail intil loaded
var counter = 0, maxLoops = 10;
nuClock();
do{
sleep(500);
counter++;
preDebug.innerText += ''r'nLoop:' + counter + " , " + time_since_last();
}while( !External.curFile.lodd && (counter < maxLoops) ); //idle and check if file loaded, 5000ms max
preDebug.innerText += ''r'nLoaded?:' + External.curFile.lodd;
preDebug.innerText += ''r'nLines?:' + External.curFile.lins;
if( External.curFile.lodd ){ // The last load operating was successful, attempt to parse and interpret each line
// parse and interpret lines, storing each meaning in 'curLineMeaning', until last line is reached
while(!External.curFile.rEOF){
curLineMeaning = meaning( s( External.readln_from_current_file() ), context);
preDebug.innerText += ''r'nNext Line?: ' + External.curFile.lnnm;
preDebug.innerText += ''r'nEOF?: ' + External.curFile.rEOF;
}
} // else, return 'null'
return curLineMeaning; // return the result of the last form
}
它调用以下内容:
External.read_file_in_load_path = function(nameStr){ // Read the lines of 'nameStr' into 'External.curFile.lins'
External.curPath.objt.getFile( // call 'DirectoryEntry.getFile' to fetch a file in that directory
nameStr,
{create: false},
function(fileEntry){ // action to perform on the fetched file, success
External.curFile.name = nameStr; // store the file name for later use
External.curFile.objt = fileEntry; // store the 'FileEntry' for later use
External.curFile.objt.file( function(file){ // Returns 'File' object associated with selected file. Use this to read the file's content.
var reader = new FileReader();
reader.onload = function(e){
External.curFile.lodd = true; // File load success
};
reader.onloadend = function(e){
//var contents = e.target.result;
// URL, split string into lines: http://stackoverflow.com/questions/12371970/read-text-file-using-filereader
External.curFile.lins = e.target.result.split(''n'); // split the string result into individual lines
};
reader.readAsText(file);
External.curFile.lnnm = 0; // Set current line to 0 for the newly-loaded file
External.curFile.rEOF = false; // Reset EOF flag
// let's try a message instead of a flag ...
/*chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});*/
} );
},
function(e){ External.curFile.lodd = false; } // There was an error
);
};
此应用程序是Scheme的方言。重要的是应用程序要知道源文件是否已加载。
我没有通读您的所有代码,但您不能启动异步活动,然后忙于等待它完成,因为JavaScript是单线程的。不管发生了什么,异步函数都要等到脚本完成当前处理后才能执行。换句话说,异步并不意味着并发。
一般来说,如果任务A是在异步任务B完成后执行的,那么你应该从B的完成回调中执行A。这是一种简单、安全的方法。任何快捷方式,为了获得更好的响应能力或简化代码,都会出现依赖性或竞争条件问题,需要大量的努力才能做到正确。即便如此,也很难证明代码在所有情况下都能在所有平台上正确运行。
相关文章:
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- Ajax文件加载和<输入>文件加载
- 使用Bootstrap将JSON文件加载到表中
- 将组合JSON文件加载到谷歌地图
- 将文本文件加载到javascript中
- jQuery Mobile样式从另一个文件加载内容
- 使用JavaScript将压缩文本文件加载到字符串中
- 数组在手动写入时有效,但从文本文件加载时无效
- 如何在打印前等待javascript文件加载twitter,并在打印后关闭选项卡
- 从文本文件加载jsondata时出现空白页
- AngularJS:如何使用ng-include将javascript文件加载到部分中
- 使用jquery将html文件加载到html中,用于离线/本地项目
- 从另一个grunt文件加载grunt任务
- 将php文件加载到弹出窗口中,并将php变量发布到该弹出窗口中(使用onclick)
- 如何使用node.js将不同的json文件加载到不同的json对象中
- 将变量从服务器上的外部文件加载到 HTML 文档中
- 节点 - 将 JSON 文件加载到代码中
- 如何使用 JavaScript/jQuery 将 XML 文件加载到 JSP/HTML 页面
- NodeJS和ExpressJS-Javascript文件加载,但CSS文件不加载;t
- D3.js:通过Force Layout从JSON文件加载多个网络