慕慕0116261
2019-07-25 15:51
<div class="daoshu"> <a href="">10:00:00</a> </div>
<script> var date = new Date(); </script>
<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>
JavaScript进阶篇
468195 学习 · 21891 问题
相似问题