<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>Latest comments for language 'XUL' on doukaku.org</title><link>http://ja.doukaku.org/lang/xul/</link><description>Latest comments for language 'XUL' on doukaku.org(long)</description><language>ja</language><lastBuildDate>Sat, 22 Nov 2008 19:46:35 -0000</lastBuildDate><item><title>zigorou's comment on ファイル更新の監視
</title><link>http://ja.doukaku.org/comment/395/</link><description>



&lt;a href="http://ja.doukaku.org/15/"&gt;ファイル更新の監視&lt;/a&gt;
(&lt;a href="http://ja.doukaku.org/15/nested/"&gt;Nested&lt;/a&gt; 
 &lt;a href="http://ja.doukaku.org/15/flatten/"&gt;Flatten&lt;/a&gt;)


&lt;hr&gt;
  Cc, Ciはそれぞれ

const Cc = Components.classes;
const Ci = Components.interfaces;

されているとして、

var fw = new FileWatch("/home/zigorou/hoge.txt", 5);
fw.watch();

で5秒おきにErrorConsoleに対して更新されてればmodified表示。

fw.unwatch()で監視止める。

でももっと奇麗に書き方ありそうな気がする。Observerがそもそもあったりして（ぇ
&lt;hr&gt;
  
    &lt;table&gt;&lt;tr&gt;&lt;td class="linenos"&gt;&lt;pre&gt; 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28&lt;/pre&gt;&lt;/td&gt;&lt;td class="code"&gt;&lt;div class="highlight"&gt;&lt;pre&gt;var FileWatch = function(filename, duration) {
  this.file = Cc[&amp;quot;@mozilla.org/file/local;1&amp;quot;].createInstance(Ci.nsILocalFile);
  this.file.initWithPath(filename);
  this.duration = duration;
};

FileWatch.prototype = {
  iid: null,
  lastTime: null,
  watch: function() {
    this.lastTime = (new Date()).getTime();
    var self = this;
    this.iid = setInterval(function() { self.watchFile(); }, this.duration * 1000);
  },
  watchFile: function() {
    if (this.file.lastModifiedTime - this.lastTime &amp;gt; 0) {
      this.log(&amp;quot;modified&amp;quot;);
    }

    this.lastTime = (new Date()).getTime();
  },
  log: function(message) {
    Cc[&amp;quot;@mozilla.org/consoleservice;1&amp;quot;].getService(Ci.nsIConsoleService).logStringMessage(message);
  },
  unwatch: function() {
    clearInterval(this.iid);
  }
};
&lt;/pre&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
    &lt;div align = "right" style="margin-right: 1em;"&gt;
      [&lt;a href="http://ja.doukaku.org/lang/xul/"&gt;
      XUL
      &lt;/a&gt;]
      [&lt;a href="http://ja.doukaku.org/comment/395/download/"&gt;
      download code
      &lt;/a&gt;]
      &lt;/a&gt;]
    &lt;/div&gt;
    &lt;hr&gt;
  

</description><guid>http://ja.doukaku.org/comment/395/</guid></item></channel></rss>