關於 ajax 回調 定時器的用法

//前臺的請求頁面
<script type="text/javascript">
<!--
var xmlHttp;
function createXMLHttpRequest(){
    if(window.ActiveXObject){
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest){
        xmlHttp = new XMLHttpRequest();
    }
}
function start(){
    createXMLHttpRequest();
    var url="getTime.php";
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange = callback;
    xmlHttp.send(null);
}
function callback(){
    if(xmlHttp.readyState == 4){
        if(xmlHttp.status == 200){
            document.getElementById("showtime").innerHTML = xmlHttp.responseText;
            setTimeout("start()",1000);
        }
    }
}

</script>


<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
<h1>Ajax動態顯示時間</h1>
<input type="button" value="開始顯示時間" id="go" onclick="start()" />
<p>當前時間:<font color="red"><span id="showtime"></span></font></p>
</body>
</html>

//後臺頁面的請求

<?php

header("cache-control:no-cache,must-revalidate");

header("Content-Type:text/html;charset=utf-8");

date_default_timezone_set('PRC');

$showTime = date('Y-m-d H:i:s');

echo $showTime;

?>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章