JavaScript实现实时更新系统时间

JavaScript实现实时更新系统时间

一、Js代码

    function getTime(){  
        str = "当前系统时间:"  
        var p = document.getElementById("sy_time");  
        time =  new Date();  
        year = time.getFullYear();  
        month = time.getMonth() + 1;  
        day = time.getDate();  
        hour = time.getHours();  
        minutes = time.getMinutes();  
        seconds = time.getSeconds();  
        str = str + year +"-"+ month +"-"+ day + " " +hour+":"+minutes+":"+seconds;  

        p.innerText = str;  

        setTimeout(getTime,1000);  
    }  


    window.onload = function(){  
        getTime();  
    }  

二、前端代码


<p id = "sy_time">p>

你可能感兴趣的:(javascript)