使用XMLHttpRequest-刷新间隔 [英] Using XMLHttpRequest - refresh intervals

查看:143
本文介绍了使用XMLHttpRequest-刷新间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于上一篇文章确实有很多目标,所以我决定创建这个目标(当时只有一个步骤).

所以我的问题是:

-用户登录页面.
-页面上有一个文本部分,任何登录的用户都可以更改.
-该部分的内容在XML内部,当用户更改内容时,XML文件将被重写.更改内容的用户有一个按钮,该按钮执行以下脚本,因此更改会立即显示.

问题是,打开页面的其他用户没有实时看到更改(或其附近的内容).我需要刷新该div,但我不知道该怎么做.例如,我想每2秒刷新一次xml的读取内容.我知道这不是很有效并且没有很多实用价值,但是我想轮流在游戏中应用此原理-因此我需要学习如何更新(仅div,而不是整个页面)

我想我应该使用setInterval(callServer,REFRESH_PERIOD_MILLIS);

但是我应该如何应用呢?如果使用它,我不会重新加载整个页面吗?



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?



  <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 :)



----

我知道这是没有逻辑的,因为我在函数内有该代码(这就是为什么如果xml更改则信息不会重新加载的原因-但这只是尝试和错误",请多多帮助.



----

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.

推荐答案

据我所知,您想制作一个Ajax Timer,该Ajax Timer根据收到的响应定期运行并更新页面的一部分.

通过setInterval(func-name, interval-span);可以很好地完成此操作.
您的代码似乎与您想要的相同.检查以下几行.

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不是函数,您应该在此处调用loadXMLDoc



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.


这篇关于使用XMLHttpRequest-刷新间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆