0

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?

alexgerst
  • 155
  • 1
  • 11
  • 1
    Any error showing up in your console? – Jeff Noel Jun 26 '13 at 17:32
  • 2
    Please don't do this. Not just AJAX, JavaScript itself is an event-based, asynchronous language. Don't try to circumvent it for convenience. – voithos Jun 26 '13 at 17:33
  • 2
    Why are you doing synchronous AJAX? That's blocking and generally bad practice, on how to avoid it see [this answer](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call/16825593#16825593) – Benjamin Gruenbaum Jun 26 '13 at 17:34
  • In a related note, the only time where it may be defensible to use synchronous AJAX is [during onbeforeunload](http://stackoverflow.com/questions/4316488/when-is-it-appropriate-to-use-synchronous-ajax). – voithos Jun 26 '13 at 17:35
  • @voithos Do you have a suggestion for how to only reload the textarea when the content in the .txt changes? – alexgerst Jun 26 '13 at 17:39
  • It should work, did you try putting logs in refreshLog function? Or something wrong with ajax call? – sabu Jun 26 '13 at 17:33

1 Answers1

5

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);
bwoebi
  • 23,637
  • 5
  • 58
  • 79
  • This did the trick. I will be changing to an async request based on the comments, but I suspect I would have had this issue with an async request as well. I will accept the answer as soon as SO lets me. Thanks! – alexgerst Jun 26 '13 at 17:42
  • @alexgerst You would, but you could prevent the browser from caching by having your response come back with no-cache headers. – Paul Jun 26 '13 at 17:46
  • 4
    Which god ? There are at least 5000 different ones. – tereško Jul 15 '13 at 10:22