0

I have a text file ("sample.txt") residing on a server, the content of which changes at random intervals (anything from 250 ms to 10 seconds). The script below reads this text file every half a second and displays the text in the div element "cont" at the bottom. Everything works perfectly... except for the fact that approximately 10% of the time the text fails to display, even though 1) the text file contains text; 2) the request.readyState == 4; and 3) the request.status == 200. Can anybody shed any light on why this might be happening? Any help would be much appreciated.

<body bgcolor=Black>

<script type='text/JavaScript'>

var int=self.setInterval("getText()",500);

function getText() {
   makerequest("sample.txt?v="+Math.random(),"cont");
   }

function makerequest(serverPage, tagID) {
   var request = get_XmlHttp();
   request.open("GET", serverPage);
   request.send(null);
   request.onreadystatechange = function() {
   if (request.readyState == 4) {
      if (request.status == 200) {
         document.getElementById(tagID).innerHTML = request.responseText; }
      } 
   }
}

function get_XmlHttp() {
   var xmlHttp = null;
   if(window.XMLHttpRequest) {
      xmlHttp = new XMLHttpRequest();
   }
   else if(window.ActiveXObject) {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   return xmlHttp;
}

</script>

<div id="cont">

</div>

</body>
  • Perhaps sometimes the HTTP request that accesses the file sees it at the very moment that the update is happening. Generally the safe way to do that (on the server) is to have the update code write the new content to another file, and then use the appropriate OS-specific "rename" to update the file. Usually, file renames are atomic operations. – Pointy Sep 01 '12 at 15:26
  • Note that it's the server code that matters here. If the JavaScript code successfully updates the view most of the time, then it's pretty unlikely to be the source of the problem. You didn't describe how the server-side updates are made. – Pointy Sep 01 '12 at 15:28
  • Contains the request.responseText special Characters? – pce Sep 01 '12 at 15:38

1 Answers1

0

It could be that in some cases the txt file is being returned by the server too quickly.

You are issuing the HTTP request (using .send()) before binding the result to a function, so if the server responds immediately the browser will not know what to do with it.

Try re-arranging the function:

function makerequest(serverPage, tagID) {
    var request = get_XmlHttp();
    request.open("GET", serverPage);
    request.onreadystatechange = function() {
        if (request.readyState = 4) {
            if (request.status == 200) {
                document.getElementById(tagID).innerHTML = request.responseText;
            }
        } 
    }
    request.send(null);
}
Steve H
  • 946
  • 9
  • 22
  • I'm afraid yours didn't help, Steve. There are no special characters in the text, just plain HTML (e.g. I could munch good dry oats.) An ASP file is creating the text file as shown below. <% Set fso = createobject("scripting.filesystemobject") dim pageName pageName = Request("PageName") Set act = fso.CreateTextFile(server.mappath(pageName), true) act.WriteLine Request("s") act.close %> – Mark Raishbrook Sep 01 '12 at 18:08
  • @Pointy: Renaming the file didn't help either, I'm afraid. – Mark Raishbrook Sep 01 '12 at 19:00
  • '<% Set fso = createobject("scripting.filesystemobject") dim pageName pageName = Request("PageName") Set act = fso.CreateTextFile(server.mappath(pageName), true) act.WriteLine Request("s") act.close %>' – Mark Raishbrook Sep 01 '12 at 19:57
  • Apologies for the non-pretty comments! I'm new to StackOverflow. Still no success, though, and I need to get this sorted PDQ. Any other suggestions? I can post the whole logic process/code if required. – Mark Raishbrook Sep 01 '12 at 23:29