【发布时间】:2014-04-23 12:56:59
【问题描述】:
我正在尝试创建一个页面,其中请求的页面在设定的时间间隔后刷新。
我获取了一些使用按钮的 w3schools 基本代码。但是,我希望脚本在不使用按钮的情况下加载和显示内容,并在设定的时间间隔后刷新。
我不想使用 Jquery,而是使用 AJAX。
这会在按下按钮后将内容加载到 div 中的页面上,但希望没有按钮单击并在 5 秒后刷新。
<script>
function loadXMLDoc()
{
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("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","content.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
【问题讨论】:
标签: javascript php ajax