You can use the underscore utility method Once that keeps the function from executing more than once. You'd have to make your code look like this:
var func = _.once(function(targetfile){
fs.readFile("./files/"+targetfile, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
if (data=="") return; //This should keep it from happening
//Updates the client here
fs.truncate("./files/"+targetfile, 0);
});
});
fs.watch("./files/", function(event, targetfile){
console.log(targetfile, 'is', event);
func(targetfile);
});
If you want it executed more than once, but you want to filter out duplicate events, you can use a function such as throttle or debounce.