Click here to Skip to main content
15,888,321 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Because the previous post I did had some many objectives, I decided to create this one (one step at the time).

So my question is:

- A user login in to a page.
- The page has a text section which any logged in user can change.
- The content of that section is inside XML and when user change the content the XML file is rewritten. The user that changes the content has a button that executes the script below, so the changes are imediatelly shown.

The problem is, other user with the page opened, dont see the change in real time (or something near it). What I need is to refresh that div and I dont know how to do that. For example I want to refresh the readed content of the xml every 2 seconds. I know this is not very efficient and hasnt many practical value, but I want to apply this principle in a game by turns - so I need to learn how to update (only the div, not the entire page)

I suppose I should use setInterval(callServer, REFRESH_PERIOD_MILLIS);

But how should I apply it? If I use it I dont reload the whole page?



XML
  <script type="text/javascript">
      function loadXMLDoc() {
          var xmlhttp;
          if (window.XMLHttpRequest) {
              xmlhttp = new XMLHttpRequest();
          }
          else {// code for IE6, IE5
              xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          }
          xmlhttp.onreadystatechange = function () {
              if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    // change content from div
                  document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
              }
          }
          xmlhttp.open("GET", "info.xml", true);
          xmlhttp.send();
      }

      // first page load
      loadXMLDoc();
      setInterval(callServer, 30);
    
</script>
      

</head>
<body>


  <div id="myDiv"><h2> <% .... content read from xml file through ajax call %></h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

is not there the input of the new content (which will be created using another ajax call to write on XML dont worry because that :)


----

I know this is no logic because I have that code inside a function (that's why the information dont reload if the xml is changed - but it was only a "try and error", appreciate any help.
Posted
Updated 24-Feb-11 7:05am
v6

1 solution

As far i understood you want to make a Ajax Timer which periodically run and Update a Section of the page based on the response received.

This is very well done through setInterval(func-name, interval-span);.
your code seems same what you want. check the following lines.

// first page load
loadXMLDoc();
setInterval(callServer, 30);


callserver is not a function you should call loadXMLDoc here

// first page load
loadXMLDoc();
setInterval(loadXMLDoc, 2000); // 2 seconds.


If it doesnt help please describe your problem with more detail.
 
Share this answer
 
Comments
Maxdd 7 26-Feb-11 13:43pm    
Oops my bad, I thought callServer with was a standard def for some postback action.

Just a question, there is a way to call multiple functions at the same period of time ?
_Ashish 26-Feb-11 14:23pm    
make a function that calls other functions. It'd be called sequentially. To call functions in parallel try using setTimeOut(). setTimeOut(f1, 2); setTimeOut(f2,2); .. so on;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900