-4
我嘗試每隔5秒刷新一次div,但它不起作用,我有3個文件。DIV自動刷新
的index.php包含:
<html>
<head>
<script type="text/javascript" src="javascript/jquery-1.3.2.js" ></script>
<script type="text/javascript" src="javascript/javascript.js" ></script>
<script type="text/javascript">
$(document).ready(function(){
setInterval ("newmsg()", 5000);
setInterval ("newpause()", 5000);
});
</script>
</head>
<body>
<div id="msgs">
<?php echo date("H:i:s"); ?>
</div>
</body>
</html>
然後,我有javascript.js包含
function GetXmlHttpObject() {
var xmlHttp = null;
try {
xmlHttp = new XMLHttpRequest();
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function newmsg(){
var request = GetXmlHttpObject();
request.open("get", "msgsprint.php", true);
request.send(null);
request.onreadystatechange = function response() {
if (request.readyState === 4) {
if(request.responseText != "false"){
$('#msgs').html(request.responseText);
}
}
}
}
function newpause(){
var request = GetXmlHttpObject();
request.open("get", "newpauseprint.php", true);
request.send(null);
request.onreadystatechange = function response() {
if (request.readyState === 4) {
if(request.responseText != "false"){
$('#newpauselist').html(request.responseText);
}
}
}
}
和我有msgsprint.php包含
<?php
echo 'ivan';
?>
莫非
有誰請告訴我我做錯了什麼。
什麼不行?你有什麼嘗試?你有錯誤信息嗎? – 2012-08-08 08:08:58
僅僅在FYI中,當使用Ajax調用時,應該避免使用'setInterval'。鑑於您的Ajax請求需要超過五秒鐘,您的呼叫將開始疊加。改爲使用'setTimeout',並在Ajax調用完成時設置5秒的新超時。這樣你可以確定他們不會堆疊。 – 2012-08-08 08:13:00
thx,沒有錯誤,只是它不刷新div。 – zonelsk 2012-08-08 08:16:04