熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

php+ajax實現頁面自動刷新時間

2013-11-15 12:36:13  來源: PHP編程 

  indexphp

  view plaincopy to clipboardprint?
<mce:script type="text/javascript"><!  
var xmlHttp;  
function createXMLHttpRequest(){  
    if(windowActiveXObject){  
        xmlHttp = new ActiveXObject("MicrosoftXMLHTTP");  
    }  
    else if(windowXMLHttpRequest){  
        xmlHttp = new XMLHttpRequest();  
    }  
}  
function start(){  
    createXMLHttpRequest();  
    var url="getTimephp";  
    xmlHttpopen("GET"urltrue);  
    xmlHttponreadystatechange = callback;  
    xmlHttpsend(null);  
}  
function callback(){  
    if(xmlHttpreadyState == ){  
        if(xmlHttpstatus == ){  
            documentgetElementById("showtime")innerHTML = xmlHttpresponseText;  
            setTimeout("start()");  
        }  
    }  
}  
// ></mce:script>  
 
 
<html>  
<head>  
<meta httpequiv="contenttype" content="text/html;charset=gb" />  
</head>  
<body>  
<h>Ajax動態顯示時間</h>  
<input type="button" value="開始顯示時間" id="go" onclick="start()" />  
<p>當前時間<font color="red"><span id="showtime"></span></font></p>  
</body>  
</html> 
<mce:script type="text/javascript"><!
var xmlHttp;
function createXMLHttpRequest(){
 if(windowActiveXObject){
  xmlHttp = new ActiveXObject("MicrosoftXMLHTTP");
 }
 else if(windowXMLHttpRequest){
  xmlHttp = new XMLHttpRequest();
 }
}
function start(){
 createXMLHttpRequest();
 var url="getTimephp";
 xmlHttpopen("GET"urltrue);
 xmlHttponreadystatechange = callback;
 xmlHttpsend(null);
}
function callback(){
 if(xmlHttpreadyState == ){
  if(xmlHttpstatus == ){
   documentgetElementById("showtime")innerHTML = xmlHttpresponseText;
   setTimeout("start()");
  }
 }
}
// ></mce:script>

  
<html>
<head>
<meta httpequiv="contenttype" content="text/html;charset=gb" />
</head>
<body>
<h>Ajax動態顯示時間</h>
<input type="button" value="開始顯示時間" id="go" onclick="start()" />
<p>當前時間<font color="red"><span id="showtime"></span></font></p>
</body>
</html>

  getTimephp

  
view plaincopy to clipboardprint?
<?php  
header("cachecontrol:nocachemustrevalidate");  
header("ContentType:text/html;charset=gb");  
$showtime = date("北京時間Y年m月d日H:i:s");  
echo $showtime;  
?> 


From:http://tw.wingwit.com/Article/program/PHP/201311/21254.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.