当我更改观看的文件时,fs.watch 触发了两次两次、当我、文件、fs

2023-09-06 05:53:52 作者:讨你半生

 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');
   });
 });

输出:

一些数据完成 X 1一些数据完成 X 2

这是我的使用错误还是..?

It is my usage fault or ..?

推荐答案

fs.watch api:

不稳定有已知的行为"" 关于重复通知.具体来说,windows 案例 是 windows 设计的结果,其中单个文件修改可以多次调用windows API is unstable has known "behaviour" with regards repeated notifications. Specifically, the windows case being a result of windows design, where a single file modification can be multiple calls to the windows API