如何让Chrome观看文件并在文件发生变化时重新加载?

时间:2017-02-22 02:15:45

标签: javascript ajax google-chrome caching

我有一个持久性网页,其中一部分显示一个不时变化的小文件。我的解决方案是每秒都向Ajax发送文件,以便我在更改后立即看到它。我这样做了:

   function loadDoc() {
     var xhttp = new XMLHttpRequest();
     xhttp.onreadystatechange = function() {
       if (this.readyState == 4 && this.status == 200) {
         document.getElementById("display").innerHTML = this.responseText;
         setTimeout(function(){loadDoc();}, 1000);
       }
     };
     xhttp.open("GET", "newstuff.html", true);
     xhttp.send();
   }

(这里是新手;这是我的第一个Ajax代码,几乎是我的第一个JavaScript。请保持温和。)

只要文件经常更改,这个效果很好!但如果没有,请求的速度越来越慢,正如我的SimpleHTTPServer的日志所示:

127.0.0.1 - - [21/Feb/2017 19:12:51] "GET /newstuff.html HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2017 19:12:52] "GET /newstuff.html HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2017 19:12:53] "GET /newstuff.html HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2017 19:12:55] "GET /newstuff.html HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2017 19:12:57] "GET /newstuff.html HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2017 19:12:59] "GET /newstuff.html HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2017 19:13:01] "GET /newstuff.html HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2017 19:13:03] "GET /newstuff.html HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2017 19:13:06] "GET /newstuff.html HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2017 19:13:09] "GET /newstuff.html HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2017 19:13:12] "GET /newstuff.html HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2017 19:13:15] "GET /newstuff.html HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2017 19:13:19] "GET /newstuff.html HTTP/1.1" 200 -
127.0.0.1 - - [21/Feb/2017 19:13:23] "GET /newstuff.html HTTP/1.1" 200 -

......它会变得更糟,直到文件发生变化,之后请求再次进入。

我认为它的Chrome缓存能够做到这一点,并且会有越来越多的退避。如果我打开Developer Tools(我设置了适当的标志),请求每秒都会发出一次。这里的其他查询谈论清除缓存---我可以以某种方式强制JavaScript吗?或者还有另一种方法吗?任何帮助表示赞赏。

0 个答案:

没有答案