如何使用FileSystem API来调试Javascript?执行顺序问题
我正在调试一个用 Javascript 编写的复杂程序。我需要观察一个大矩阵的变化。使用 Chrome Inspect Element 查看矩阵中的每个元素并不方便。所以,我想将数据写入文本文件。我找到了文件系统 API 和终端。
我已将 FileSystem API 集成到我的项目中,请参阅 FileSystem 终端项目。我定义了一个全局变量来存储 fs.root 。我想要的是在调试时将此变量传递到我的程序中,这样我就可以使用此 fs.root 将数据转储到文本文件中。 我请求文件系统:
window.requestFileSystem(window.TEMPORARY, 5*1024*1024, onInitFs, errorHandler); // 5MB
但是“onInitFs”函数似乎是一个消息响应函数,调用得很晚。即使在“onLoad='MyFun();'”之后。所以,我不知道在哪里放置我自己的函数,以确保定义了变量“fs.root”。现在,我已经将“MyFun()”放在任何地方,所有这些都会生成“fs.root”未定义的错误,因为“onInitFs”函数没有被调用。 我已经测试了调用顺序:
-------------main.html
<html>
<header>
<script type='text/javascript' src='MyFun.js'></script>
<script type='text/javascript' >
console.log('01Position');
function onInitFs(fs)
{
aGlobalFsRoot = fs.root;
console.log('04Position');
}
window.requestFileSystem(window.TEMPORARY, 5*1024*1024, onInitFs, errorHandler);
</script>
</header>
<body onLoad="MyFun();">
<script>
console.log('02Position');
</script>
</body>
</html>
-------------MyFun.js
var aGlobalFsRoot;
function MyFun()
{
console.log('03Position');
// want to use "aGlobalFsRoot" to dump some matrix data, but it is not defined, which means "onInitFs()" is still not called.
}
因此,在 Chrome Inspect Element 的控制台窗口中: 01职位 02职位 03职位 04Position
我可以启用比“MyFun()”更早调用的“onInitFun()”函数吗?或者我应该把“MyFun()”放在哪里,这样它就可以在“onInitFun()”之后被调用。我不希望用户单击一个按钮,因为 MyFun 只是在加载时进行预处理工作。我可以生成一条消息,以便“MyFun()”将晚于“onInitFs()”被调用吗?
I was debugging a complicated program written in Javascript. I need to watch the change of a large matrix. It is not convenient to see each elements in the matrix using Chrome Inspect Element. So, I want to write the data into a text File. I found the FileSystem API and terminal.
I have integrate FileSystem API into my project, with refer to the FileSystem terminal project. And I define a global variable to store the fs.root . What I want is to pass this variable into my program when debugging, so I can dump data into text file using this fs.root.
I request file system:
window.requestFileSystem(window.TEMPORARY, 5*1024*1024, onInitFs, errorHandler); // 5MB
But the "onInitFs" function seems like a message response function, which is called very late. Even after the "onLoad='MyFun();'". So, I do not know where to put my own function, to make sure that the variable "fs.root" is defined. Right now, I have put "MyFun()" to everywhere, all will generate an error that "fs.root" is not defined, since "onInitFs" function is not called.
I have tested the calling sequence:
-------------main.html
<html>
<header>
<script type='text/javascript' src='MyFun.js'></script>
<script type='text/javascript' >
console.log('01Position');
function onInitFs(fs)
{
aGlobalFsRoot = fs.root;
console.log('04Position');
}
window.requestFileSystem(window.TEMPORARY, 5*1024*1024, onInitFs, errorHandler);
</script>
</header>
<body onLoad="MyFun();">
<script>
console.log('02Position');
</script>
</body>
</html>
-------------MyFun.js
var aGlobalFsRoot;
function MyFun()
{
console.log('03Position');
// want to use "aGlobalFsRoot" to dump some matrix data, but it is not defined, which means "onInitFs()" is still not called.
}
So, in the console window of Chrome Inspect Element:
01Position
02Position
03Position
04Position
can I enable the "onInitFun()" function called ealier than "MyFun()". Or Where should I put "MyFun()", so it can be called later than "onInitFun()". I do not want user to click one button, since MyFun just do pre-processing work when loading. May I generate one message, so "MyFun()" will be called later than "onInitFs()"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Andrey 的评论或以下内容应该有效:
据我了解,fs api 被故意设计为异步。所以你不能强迫它以任何顺序被调用。因此,您必须适应异步思维方式而不是同步思维方式。
如果您希望在文件系统请求完成时调用
MyFun
,则挂钩MyFun
在文件系统请求完成时运行(而不是其他不相关的<代码>onBodyLoad事件):Either Andrey's comment, or the following, should work:
As I understand, the fs api is deliberately designed to be asynchronous. So you can't force it to be called in any order. So you would have to adjust to an asynchronous mindset instead of a synchronous mindset.
If you want
MyFun
to be called when the file system request is done, then hookMyFun
to run when the file system request is done (and not to the other, irreleventonBodyLoad
event):