Php 在不使用jquery的情况下使用ajax每两秒刷新一次div

Php 在不使用jquery的情况下使用ajax每两秒刷新一次div,php,ajax,Php,Ajax,我做了一个聊天网站。它有一个div和id=chatlogs。我希望它每2秒从logs.php获取一次聊天日志。我该怎么做?我希望使用ajax,避免使用jquery。您可以这样做: <script> setInterval(refresh_logs(), 2000); // 2000 = 2 Seconds function refresh_logs() { var xmlhttp; if (window.XMLHttpRequest)

我做了一个聊天网站。它有一个
div
id=chatlogs
。我希望它每2秒从logs.php获取一次聊天日志。我该怎么做?我希望使用ajax,避免使用jquery。

您可以这样做:

<script>
    setInterval(refresh_logs(), 2000); // 2000 = 2 Seconds

   function refresh_logs()
   {
      var xmlhttp;
      if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
        }
      else
        {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      xmlhttp.onreadystatechange=function()
        {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
           document.getElementById("logs_div").innerHTML=xmlhttp.responseText;
          }
        }
      xmlhttp.open("POST","get_logs.php",true);
      xmlhttp.send();
   }
   </script>

setInterval(刷新日志(),2000);//2000=2秒
函数刷新日志()
{
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“logs_div”).innerHTML=xmlhttp.responseText;
}
}
open(“POST”,“get_logs.php”,true);
xmlhttp.send();
}

setInterval(ajax,2000)-其中ajax是发送和处理请求的完整功能,我被卡住了,等待你们帮我做一个自动刷新。你能更深入地解释一下RamRaider吗?谢谢您的建议。@Bido262请立即查看。刚刚添加了纯Javascript ajax。这对你有用。你可以去掉activeX对象吗。Im使用更新版本的浏览器。我不需要activeXobject,请删除它们。因为如果我自己删除它们,我会被搞砸的。提前谢谢,请删除它们。我知道你正在使用更新版本,但有些用户不使用最新版本。所以他们可能会面临旧版本的问题。如果这个代码对你有用,那么就这样保存它。谢谢你帮我。