fs.watch fired twice when I change the watched file

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

    I make allowance for this by doing the following:

    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
        }
    }
    

提交回复
热议问题