JS計時器 年月日時分秒幾十

計時器功能代碼:

<html>
<body>

<script type="text/javascript">

//設置起始時間
var together = new Date();
together.setFullYear(2015,9,17); //時間年月日     注意這個9代表的是10月
together.setHours(0);//小時   
together.setMinutes(00);//分鐘
together.setSeconds(0); //秒前一位
together.setMilliseconds(2);//秒第二位

//使用定時器調用這個函數 就可實現
timeElapse(together);

function timeElapse(date){
    var current = Date();
    var seconds = (Date.parse(current) - Date.parse(date)) / 1000;
    var days = Math.floor(seconds / (3600 * 24));
    seconds = seconds % (3600 * 24);
    var hours = Math.floor(seconds / 3600);
    if (hours < 10) {
        hours = "0" + hours;
    }
    seconds = seconds % 3600;
    var minutes = Math.floor(seconds / 60);
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    seconds = seconds % 60;
    if (seconds < 10) {
        seconds = "0" + seconds;
    }
    var result = "第 <span class=\"digit\">" + days + "</span> 天 <span class=\"digit\">" + hours + "</span> 小時 <span class=\"digit\">" + minutes + "</span> 分鐘 <span class=\"digit\">" + seconds + "</span> 秒"; 


document.write(result)
}

</script>

</body>
</html>

定時器在哪調用自行添加。

發佈了97 篇原創文章 · 獲贊 41 · 訪問量 73萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章