Reputation: 155
I am attempting to have an HTML page update a textarea with content from a text file every second, using JavaScript's setInterval function. However, the function inside the setInterval call only seems to run once.
Javascript:
// Send a GET request to the given location
function sendRequest(location, nonblocking) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", location, nonblocking);
xmlhttp.send();
return xmlhttp.responseText;
}
// Refresh the communication log
function refreshLog() {
document.getElementById("comm_log").value = sendRequest("src/log.txt", false);
}
window.setInterval(refreshLog, 1000);
The request is not asynchronous simply because the text file will never be long, and this is something I am trying to throw together quickly.
HTML:
<html>
<head>
<style type="text/css">
textarea {
width: 98%;
height: 80%;
resize: none;
font-family: "Courier New";
}
</style>
<script type="text/javascript" src="src/script.js"></script>
</head>
...
<textarea id="comm_log" readonly></textarea>
...
</html>
Anyone have ideas?
Upvotes: 0
Views: 919
Reputation: 23777
I suppose your browser is caching. You get the data exactly once and then the next requests are served from cache. So the refreshLog
should be called repeatedly, just the following calls won't have any effect on the page as it's serving from cache.
Append to the URL some unique part like a ?=<timestamp>
.
xmlhttp.open("GET", location + '?=' + new Date().getTime(), nonblocking);
Upvotes: 5