Home > Web Front-end > JS Tutorial > body text

js gets server real time

小云云
Release: 2018-03-26 16:35:43
Original
2456 people have browsed it

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!=&#39;function&#39;) return;
        var ajaxObject;
        try{
            ajaxObject=new XMLHttpRequest();
        }catch(e){
            try{
                ajaxObject=new ActiveXObject(&#39;Microsoft.XMLHTTP&#39;);
            }catch(e){
            }
        }
        if(!ajaxObject) return;
        if(ajaxObject.overrideMimeType){
            ajaxObject.overrideMimeType(&#39;text/html&#39;);
        }
        //location.href可以换成其他url,但必须是同一个站点的链接,并且文件存在
        ajaxObject.open(&#39;get&#39;,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(&#39;Date&#39;));
                _timestamp=_timestamp.toString().match(/^\d$/)?_timestamp:new Date().getTime();
                //设置定时器每过一秒动态刷新一次时间
                setInterval(
                    function(){
                        //这里可以自定义时间显示格式
                        document.getElementById(&#39;_timer&#39;).innerHTML=new Date(_timestamp).toLocaleString();
                        _timestamp+=1000;
                    },
                    1000
                );
            }
        );
    }
    window.onload=getTime;
</script>
</head>
<body>
    <p id="_timer">正在获取服务器时间……</p>
</body>
</html>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!