fs.watch fired twice when I change the watched file

后端 未结 12 1522
星月不相逢
星月不相逢 2020-12-09 01:30
 fs.watch( \'example.xml\', function ( curr, prev ) {
   // on file change we can read the new xml
   fs.readFile( \'example.xml\',\'utf8\', function ( err, data ) {         


        
12条回答
  •  一个人的身影
    2020-12-09 02:10

    Similar/same problem. I needed to do some stuff with images when they were added to a directory. Here's how I dealt with the double firing:

    var fs = require('fs');
    var working = false;
    
    fs.watch('directory', function (event, filename) {
      if (filename && event == 'change' && active == false) {
        active = true;
    
        //do stuff to the new file added
    
        active = false;
    });
    

    It will ignore the second firing until if finishes what it has to do with the new file.

提交回复
热议问题