Jquery 在grails中,每个时间间隔刷新一个div内容

Jquery 在grails中,每个时间间隔刷新一个div内容,jquery,ajax,Jquery,Ajax,如何做到这一点,以便div每隔5秒自动刷新一次,或者如果数据库中有更改。使用ajax?或者jquery?将运行函数更新div的间隔设置为5000。 签出以下链接: 假设您有一个id为my_div的div,您希望通过从my_page.php请求每隔5秒刷新它的内容 客户端JavaScript代码: <html> <head> </head> <body> <div id="my_div"></div> </body&g

如何做到这一点,以便
div
每隔5秒自动刷新一次,或者如果数据库中有更改。使用ajax?或者jquery?

将运行函数更新div的间隔设置为5000。 签出以下链接:

假设您有一个id为
my_div
的div,您希望通过从
my_page.php
请求每隔5秒刷新它的内容

客户端JavaScript代码:

<html>
<head>
</head>
<body>
<div id="my_div"></div>
</body>
  <script type="text/javascript">
  var refresh_time = 5000;

  /** To make sure that program will execute when everything loaded in page. **/
  window.onload = function(){

    /** setInterval() will calls my_div_update() function in refresh times. **/
    setInterval(function(){my_div_update();}, refresh_time);

  }

  /** Uses Ajax request to update my_div content. **/
  function my_div_update()
    {
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
      {
      /** To make sure everything is ok on the way. **/
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("my_div").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","my_page.php");
    xmlhttp.send();
    }
  </script>
</html>

var刷新时间=5000;
/**确保在页面中加载所有内容时程序将执行**/
window.onload=函数(){
/**setInterval()将在刷新时调用my_div_update()函数**/
setInterval(函数(){my_div_update();},刷新时间);
}
/**使用Ajax请求更新my_div内容**/
函数my_div_update()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数()
{
/**确保路上一切正常**/
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“my_div”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“my_page.php”);
xmlhttp.send();
}
服务器端PHP代码:

<?php echo date('h:i:s - A'); ?>


这样行得通,但这样好吗?我的意思是它每5秒就刷新一次。是否有某种代码用于检查数据库中是否有更改,然后它将刷新http世界中的所有内容都以请求开始,因此您“必须”将请求发送到服务器页面以查看更改。您能为我指明正确的方向吗?或者给我一个指导?