avi*_*abs 28 watch node.js windows-8
fs.watch( 'example.xml', function ( curr, prev ) {
// on file change we can read the new xml
fs.readFile( 'example.xml','utf8', function ( err, data ) {
if ( err ) throw err;
console.dir(data);
console.log('Done');
});
});
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
- 一些数据
- 完成X 1
- 一些数据
- 完成X 2
这是我的使用错误还是..?
Per*_* P. 40
该fs.watchAPI:
小智 17
我通过以下方式考虑到这一点:
var fsTimeout
fs.watch('file.js', function(e) {
if (!fsTimeout) {
console.log('file.js %s event', e)
fsTimeout = setTimeout(function() { fsTimeout=null }, 5000) // give 5 seconds for multiple events
}
}
Run Code Online (Sandbox Code Playgroud)
Art*_*n72 13
我建议使用chokidar(https://github.com/paulmillr/chokidar),它比以下更好fs.watch:
评论其README.md:
Node.js fs.watch:
rename.Node.js fs.watchFile:
Jan*_*cki 10
如果您需要查看文件以进行更改,那么您可以查看我的小型库文件更改.它检查触发change事件之间的文件sha1哈希.
解释为什么我们有多个被激活的事件:
在某些情况下,您可能会注意到单个创建事件会生成由组件处理的多个Created事件.例如,如果使用FileSystemWatcher组件来监视目录中新文件的创建,然后使用记事本创建文件进行测试,即使只创建了一个文件,也可能会看到生成两个Created事件.这是因为Notepad在写入过程中执行多个文件系统操作.记事本批量写入磁盘,创建文件的内容,然后创建文件属性.其他应用程序可以以相同的方式执行.由于FileSystemWatcher监视操作系统活动,因此将拾取这些应用程序触发的所有事件.
我个人喜欢使用return来防止在检查某些内容时运行代码块,所以,这是我的方法:
var watching = false;
fs.watch('./file.txt', () => {
if(watching) return;
watching = true;
// do something
// the timeout is to prevent the script to run twice with short functions
// the delay can be longer to disable the function for a set time
setTimeout(() => {
watching = false;
}, 100);
};
Run Code Online (Sandbox Code Playgroud)
请随意使用此示例来简化您的代码。它可能并不比使用其他人的模块更好,但它工作得很好!
| 归档时间: |
|
| 查看次数: |
27326 次 |
| 最近记录: |