I have a persistent web page, part of which displays a small file that changes from time to time. My solution was to Ajax that file in every second, so that I see it soon after it changes. I did this:
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();
}
(Newbie here; this is my first Ajax code, almost my first JavaScript. Be gentle.)
This works great, as long as the file changes frequently! But if not, the requests come in more and more slowly, as shown by the log of my 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 -
...and it just gets worse, until the file changes, after which the requests come in every second again.
I presume that it's Chrome's cache that's doing this, with a growing backoff. The requests come every second if I have Developer Tools open (I set the appropriate flag). Other queries here talk about clearing the cache---can I force that from JavaScript somehow? Or is there another way to do this? Any help appreciated.