为什么fs.watchFile在Node中调用了两次?
Ubuntu 12.04
Node v0.6.14
CoffeeScript 1.3.1
fs.watchFile coffee_eval, (e) ->
console.log e
result = spawn 'coffee', ['-bc', coffee_eval]
msg = ''
result.stderr.on 'data', (str) ->
msg+= str
result.stderr.on 'end', ->
console.log 'msg: ', msg
print "!! #{coffee_eval}\n"
要点上的整个代码:
每次我保存一个被监视的文件时,主函数被调用两次而不是一次。 我的编辑是Sublime Text 2。
输出字可以看出:
回答如下:fs.watchFile不稳定。从节点文档:
fs.watchFile(filename,[options],listener)#
稳定性:2 - 不稳定。如果可用,请改用fs.watch。
你可以尝试fs.watch
,但不幸的是它可能会遇到同样的问题。当我试图创建一个类似的fs.watch
时,我在Windows上与monitor script有同样的问题。
解决方法是记录修改发生的时间,如果在几毫秒内触发,则忽略第二个更改。有点难看,但它的确有效。
为什么fs.watchFile在Node中调用了两次?
Ubuntu 12.04
Node v0.6.14
CoffeeScript 1.3.1
fs.watchFile coffee_eval, (e) ->
console.log e
result = spawn 'coffee', ['-bc', coffee_eval]
msg = ''
result.stderr.on 'data', (str) ->
msg+= str
result.stderr.on 'end', ->
console.log 'msg: ', msg
print "!! #{coffee_eval}\n"
要点上的整个代码:
每次我保存一个被监视的文件时,主函数被调用两次而不是一次。 我的编辑是Sublime Text 2。
输出字可以看出:
回答如下:fs.watchFile不稳定。从节点文档:
fs.watchFile(filename,[options],listener)#
稳定性:2 - 不稳定。如果可用,请改用fs.watch。
你可以尝试fs.watch
,但不幸的是它可能会遇到同样的问题。当我试图创建一个类似的fs.watch
时,我在Windows上与monitor script有同样的问题。
解决方法是记录修改发生的时间,如果在几毫秒内触发,则忽略第二个更改。有点难看,但它的确有效。