課程
/前端開發
/JavaScript
/JavaScript進階篇
<div?class="daoshu"> ????<a?href="">10:00:00</a> </div>
<script> ????var?date?=?new?Date(); </script>
2019-07-25
源自:JavaScript進階篇 8-3
正在回答
<h1?id="text"></h1> <script> ????var?toDate; ????toDate?=?new?Date(); ????toDate.setDate(toDate.getDate()+1); ????toDate.setHours(10); ????//?獲取明天上午10點時間戳 ????toDate?=?toDate.getTime(); ????var?t?=?document.getElementById('text'); ????setInterval(function(){ ????????//?獲取現在時間戳 ????????now?=?new?Date().getTime(); ????????t.innerText?=?"距離明天上午10點,還有"?+?Math.ceil((toDate?-?now)?/?1000)?+?"秒"; ????},?1000); </script>
如果想要時分秒形式,自己把秒換算一下就行
????var?attime; ????function?clock()?{ ????????var?time?=?new?Date(); ???????? ????????var?hour?=?time.getHours(); ????????var?minute?=?time.getMinutes(); ????????var?second?=?time.getSeconds(); ???????? ????????hour?=?hour?>?9???hour?:?"0"?+?hour; ????????minute?=?minute?>?9???minute?:?"0"?+?minute; ????????second?=?second?>?9???second?:?"0"?+?second; ???????? ????????attime=?hour?+?":"?+?minute?+?":"?+?second; ????????document.getElementById("clock").value?=?attime; ????}
我覺得可以把這個字符先轉成時間秒,然后遞減該數值,顯示字符的時候把數字再轉成時分秒的格式,其他應該還有更加簡單高效的辦法
??<script> ??????????var?hours?=?10;??????? ???????????var?minutes?=?00;???????? ???????????var?seconds?=?00;???????? ???????????var?total?=?hours?*?3600?+?minutes?*?60?+?seconds;???????? ???????????function?countdown()?{???????????? ???????????????var?a?=?document.getElementById("countdown");???????????? ???????????????timestr?=?lessTen(Math.floor(total?/?3600))?+?":"?+?lessTen(Math.floor(total?/?60)?%?60)?+?":"?+?lessTen(total?%?60); ???????????????a.innerHTML?=?timestr;????????????total--;????????}???????????????? ???????????function?lessTen(n){???????????? ???????????????if?(n<10?&&?n?>=0){???????????????? ???????????????return??"0"?+?n;???????????? ???????????}???????????? ???????????return?n;???????? ???????????}???????? ???????????setInterval(countdown,?1000);??? ????????????</script>
慕粉1474727424
老張家有女神
舉報
本課程從如何插入JS代碼開始,帶您進入網頁動態交互世界
1 回答請問大神,如何把計數時間格式顯示為00:00:00,,,如果能的話,怎么可以stop一次然后輸出一個計時時間,就像那個跑步計時器一樣的功能。
3 回答為什么mydate.setTime( 0 )輸出的時間里 小時是08:00?
3 回答怎么顯示00-09
2 回答為什么初始值要為1呢?不是應該為0 嗎?
1 回答時間倒數不對路,直接跳過中間的數值到NaN了?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2019-08-05
<h1?id="text"></h1> <script> ????var?toDate; ????toDate?=?new?Date(); ????toDate.setDate(toDate.getDate()+1); ????toDate.setHours(10); ????//?獲取明天上午10點時間戳 ????toDate?=?toDate.getTime(); ????var?t?=?document.getElementById('text'); ????setInterval(function(){ ????????//?獲取現在時間戳 ????????now?=?new?Date().getTime(); ????????t.innerText?=?"距離明天上午10點,還有"?+?Math.ceil((toDate?-?now)?/?1000)?+?"秒"; ????},?1000); </script>如果想要時分秒形式,自己把秒換算一下就行
2019-09-09
????var?attime; ????function?clock()?{ ????????var?time?=?new?Date(); ???????? ????????var?hour?=?time.getHours(); ????????var?minute?=?time.getMinutes(); ????????var?second?=?time.getSeconds(); ???????? ????????hour?=?hour?>?9???hour?:?"0"?+?hour; ????????minute?=?minute?>?9???minute?:?"0"?+?minute; ????????second?=?second?>?9???second?:?"0"?+?second; ???????? ????????attime=?hour?+?":"?+?minute?+?":"?+?second; ????????document.getElementById("clock").value?=?attime; ????}2019-07-26
我覺得可以把這個字符先轉成時間秒,然后遞減該數值,顯示字符的時候把數字再轉成時分秒的格式,其他應該還有更加簡單高效的辦法
??<script> ??????????var?hours?=?10;??????? ???????????var?minutes?=?00;???????? ???????????var?seconds?=?00;???????? ???????????var?total?=?hours?*?3600?+?minutes?*?60?+?seconds;???????? ???????????function?countdown()?{???????????? ???????????????var?a?=?document.getElementById("countdown");???????????? ???????????????timestr?=?lessTen(Math.floor(total?/?3600))?+?":"?+?lessTen(Math.floor(total?/?60)?%?60)?+?":"?+?lessTen(total?%?60); ???????????????a.innerHTML?=?timestr;????????????total--;????????}???????????????? ???????????function?lessTen(n){???????????? ???????????????if?(n<10?&&?n?>=0){???????????????? ???????????????return??"0"?+?n;???????????? ???????????}???????????? ???????????return?n;???????? ???????????}???????? ???????????setInterval(countdown,?1000);??? ????????????</script>