fs.watch(filename[, options][, listener])


监视 filename 的更改,其中 filename 可以是文件或目录。

【Watch for changes on filename, where filename is either a file or a directory.】

第二个参数是可选的。如果将 options 提供为字符串,它指定 encoding。否则,options 应作为对象传递。

【The second argument is optional. If options is provided as a string, it specifies the encoding. Otherwise options should be passed as an object.】

监听器回调函数接收两个参数 (eventType, filename)eventType 可以是 'rename''change',而 filename 是触发该事件的文件名。

【The listener callback gets two arguments (eventType, filename). eventType is either 'rename' or 'change', and filename is the name of the file which triggered the event.】

在大多数平台上,每当目录中出现或消失一个文件名时,都会触发 'rename'

【On most platforms, 'rename' is emitted whenever a filename appears or disappears in the directory.】

监听器回调附加到由 <fs.FSWatcher> 触发的 'change' 事件,但这与 eventType'change' 值并不相同。

【The listener callback is attached to the 'change' event fired by <fs.FSWatcher>, but it is not the same thing as the 'change' value of eventType.】

如果传入 signal,中止相应的 AbortController 将关闭返回的 <fs.FSWatcher>

【If a signal is passed, aborting the corresponding AbortController will close the returned <fs.FSWatcher>.】