ajax实现自动刷新页面实例
发布时间:2020-12-16 01:40:08 所属栏目:百科 来源:网络整理
导读:html部分: !DOCTYPEhtmlhtmllang="en"headmetacharset="UTF-8"titleajax实现自动刷新/title/headbodyonLoad="Autofresh()"p现在的时间是:spanid="currenttime"/span/pscriptvarxmlobj;varcount=0;functioncreateXMLHttpRequest(){if(window.ActiveXObject)
html部分: <!DOCTYPEhtml> <htmllang="en"> <head> <metacharset="UTF-8"> <title>ajax实现自动刷新</title> </head> <bodyonLoad="Autofresh()"> <p>现在的时间是:<spanid="currenttime"></span></p> <script> varxmlobj; varcount=0; functioncreateXMLHttpRequest(){ if(window.ActiveXObject){ xmlobj=newActiveXObject("Microsoft.XMLHTTP"); } elseif(window.XMLHttpRequest){ xmlobj=newXMLHttpRequest(); } } functionAutofresh(){ createXMLHttpRequest(); count=count+1; xmlobj.open("GET","currenttime.php?count="+count,true); xmlobj.onreadystatechange=doAjax; xmlobj.send("r="+Math.random());//使用随机数处理缓存 } functiondoAjax(){ if(xmlobj.readyState==4&&xmlobj.status==200){ vartime_span=document.getElementById('currenttime'); time_span.innerHTML=xmlobj.responseText; setTimeout("Autofresh()",2000); } } </script> </body> </html> php页面部分 <?php $count=$_GET["count"]; $count=$count%7; switch($count){ case1:$message="22222222222222211";break; case2:$message="22222222222222222";break; case3:$message="33333333333333333";break; case4:$message="44444444444444444";break; case5:$message="55555555555555555";break; case6:$message="66666666666666666";break; } $res=$message; echodate("Y-m-dH:i:s")."<hr>"."现在的内容是:".$res; ?> 效果图: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |