每隔一段时间刷新一次div内容 [英] Refresh a div content every time interval in grails
本文介绍了每隔一段时间刷新一次div内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
该如何执行,以使div
每5秒自动刷新一次,或者数据库中是否有更改.用ajax?还是jquery?
How do you do that so that a div
refreshes say every 5 seconds automatically or if there are changes in the database. with ajax? or jquery?
推荐答案
在5000上设置一个运行函数更新div的时间间隔. 结帐以下链接: http://www.w3schools.com/js/js_timing.asp
Set an interval on 5000 that runs a function update div. checkout following link: http://www.w3schools.com/js/js_timing.asp
假设您有一个ID为my_div
的div,并且您想通过按my_page.php
的请求每5秒刷新一次它的内容.
Let's say you have a div with id of my_div
and you want refresh it's content every 5 seconds by requesting from my_page.php
.
客户端JavaScript代码:
Client-Side JavaScript Code:
<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>
服务器端PHP代码:
<?php echo date('h:i:s - A'); ?>
这篇关于每隔一段时间刷新一次div内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文