关于 ajax 回调 定时器的用法

前端之家收集整理的这篇文章主要介绍了关于 ajax 回调 定时器的用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. //前台的请求页面
  2. <scripttype="text/javascript">
  3. <!--
  4. varxmlHttp;
  5. functioncreateXMLHttpRequest(){
  6. if(window.ActiveXObject){
  7. xmlHttp=newActiveXObject("Microsoft.XMLHTTP");
  8. }
  9. elseif(window.XMLHttpRequest){
  10. xmlHttp=newXMLHttpRequest();
  11. }
  12. }
  13. functionstart(){
  14. createXMLHttpRequest();
  15. varurl="getTime.PHP";
  16. xmlHttp.open("GET",url,true);
  17. xmlHttp.onreadystatechange=callback;
  18. xmlHttp.send(null);
  19. }
  20. functioncallback(){
  21. if(xmlHttp.readyState==4){
  22. if(xmlHttp.status==200){
  23. document.getElementById("showtime").innerHTML=xmlHttp.responseText;
  24. setTimeout("start()",1000);
  25. }
  26. }
  27. }
  28.  
  29. </script>
  30.  
  31.  
  32. <html>
  33. <head>
  34. <Metahttp-equiv="content-type"content="text/html;charset=utf-8"/>
  35. </head>
  36. <body>
  37. <h1>Ajax动态显示时间</h1>
  38. <inputtype="button"value="开始显示时间"id="go"onclick="start()"/>
  39. <p>当前时间:<fontcolor="red"><spanid="showtime"></span></font></p>
  40. </body>
  41. </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;

?>

猜你在找的Ajax相关文章