fs.watch fired twice when I change the watched file

后端 未结 12 1520
星月不相逢
星月不相逢 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:13

    I somtimes get multible registrations of the Watch event causing the Watch event to fire several times. I solved it by keeping a list of watching files and avoid registering the event if the file allready is in the list:

     var watchfiles = {};
    
    function initwatch(fn, callback) {
        if watchlist[fn] {
            watchlist[fn] = true;
            fs.watch(fn).on('change', callback);
        }
    }
    

    ......

提交回复
热议问题