Home >Web Front-end >JS Tutorial >js gets server real time
This article mainly shares with you js to obtain the real-time time of the server. It mainly shares with you the code method. I hope it can help everyone.
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>AJAX获取服务器时间</title> <script type="text/javascript"> /* * ajax 函数,处理 ajax 请求 * @param function callback 回调函数 * @因为要和服务器交互,所以必须在服务器环境运行,不能在硬盘上直接打开 */ function ajax(callback){ if(typeof callback!='function') return; var ajaxObject; try{ ajaxObject=new XMLHttpRequest(); }catch(e){ try{ ajaxObject=new ActiveXObject('Microsoft.XMLHTTP'); }catch(e){ } } if(!ajaxObject) return; if(ajaxObject.overrideMimeType){ ajaxObject.overrideMimeType('text/html'); } //location.href可以换成其他url,但必须是同一个站点的链接,并且文件存在 ajaxObject.open('get',location.href); ajaxObject.send(null); ajaxObject.onreadystatechange=function(){ if(ajaxObject.readyState==4){ if(ajaxObject.status==200){ callback(ajaxObject); } } }; } /* * 获取时间并动态刷新 */ function getTime(){ ajax( function(ao){ //只需要AJAX一次,将服务器时间获取后以毫米为单位保存到一个变量中 _timestamp=Date.parse(ao.getResponseHeader('Date')); _timestamp=_timestamp.toString().match(/^\d$/)?_timestamp:new Date().getTime(); //设置定时器每过一秒动态刷新一次时间 setInterval( function(){ //这里可以自定义时间显示格式 document.getElementById('_timer').innerHTML=new Date(_timestamp).toLocaleString(); _timestamp+=1000; }, 1000 ); } ); } window.onload=getTime; </script> </head> <body> <p id="_timer">正在获取服务器时间……</p> </body> </html>
Related recommendations:
js high-efficiency server time synchronization example
The above is the detailed content of js gets server real time. For more information, please follow other related articles on the PHP Chinese website!