fs.watch fired twice when I change the watched file

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

    Easiest solution:

    const watch = (path, opt, fn) => {
      var lock = false
      fs.watch(path, opt, function () {
        if (!lock) {
          lock = true
          fn()
          setTimeout(() => lock = false, 1000)
        }
      })
    }
    watch('/path', { interval: 500 }, function () {
      // ...
    })
    

提交回复
热议问题