如何执行此操作,以便div
刷新自动每5秒说一次,或者数据库中是否有更改。用ajax?还是jquery?
答案 0 :(得分:1)
设置运行函数更新div的5000的间隔。 结帐以下链接: http://www.w3schools.com/js/js_timing.asp
假设您有一个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>
服务器端PHP代码:
<?php echo date('h:i:s - A'); ?>