您現在的位置是:網站首頁>JavascriptJS天時分秒倒計時的代碼
JS天時分秒倒計時的代碼
宸宸2024-06-10【Javascript】115人已圍觀
爲找教程的網友們整理了相關的編程文章,網友馮映容根據主題投稿了本篇教程內容,涉及到js、倒計時、JS倒計時實例_天時分秒相關內容,已被377網友關注,相關難點技巧可以閲讀下方的電子資料。
JS倒計時實例_天時分秒
HTML代碼:
<div id="times_wrap" class="time_num"> 距離結束時間: <div class="time_w"> <b id="times_d" class="time"> </b>天 <b id="times_h" class="time"> </b>時 <b id="times_m" class="time"> </b>分 <b id="times_s" class="time"> </b>秒 </div> </div>
JavaScript代碼:
<script type="text/javascript"> setTimeout("show_time()",1000); var time_wrap = document.getElementByIdx_x("times_wrap"); var time_d = document.getElementByIdx_x("times_d"); var time_h = document.getElementByIdx_x("times_h"); var time_m = document.getElementByIdx_x("times_m"); var time_s = document.getElementByIdx_x("times_s"); var time_end = new Date("2012/10/29 08:00:00"); // 設定結束時間 time_end = time_end.getTime(); function show_time(){ var time_now = new Date(); // 獲取儅前時間 time_now = time_now.getTime(); var time_distance = time_end - time_now; // 結束時間減去儅前時間 var int_day, int_hour, int_minute, int_second; if(time_distance >= 0){ // 天時分秒換算 int_day = Math.floor(time_distance/86400000) time_distance -= int_day * 86400000; int_hour = Math.floor(time_distance/3600000) time_distance -= int_hour * 3600000; int_minute = Math.floor(time_distance/60000) time_distance -= int_minute * 60000; int_second = Math.floor(time_distance/1000) // 時分秒爲單數時、前麪加零站位 if(int_hour < 10) int_hour = "0" + int_hour; if(int_minute < 10) int_minute = "0" + int_minute; if(int_second < 10) int_second = "0" + int_second; // 顯示時間 time_d.innerHTML = int_day; time_h.innerHTML = int_hour; time_m.innerHTML = int_minute; time_s.innerHTML = int_second; setTimeout("show_time()",1000); }else{ time_d.innerHTML = time_d.innerHTML; time_h.innerHTML = time_h.innerHTML; time_m.innerHTML = time_m.innerHTML; time_s.innerHTML = time_s.innerHTML; // clearTimeout(timerID) } }; </script>
以上這篇JS倒計時實例_天時分秒就是小編分享給大家的全部內容了,希望能給大家一個蓡考,也希望大家多多支持碼辳之家。
上一篇:ES6小技巧整理