1. 程式人生 > >js寫距離明天秒數

js寫距離明天秒數

ear 今天 switch item set ack int new date

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>今天距離明天倒計時秒數</title>
    <style>
        div{
            width: 200px;
            height: 100px;
            border: 1px solid black;
            display: flex;
            align
-items: center; justify-content: center; } p{ margin: 0; text-align: center; } </style> </head> <body> <div> <section> <p>距離明天</p> <p>還有<span class="time">4520</span>秒</p> </section> </div> <script> var
Debugger = function(){}; // 控制臺按鈕 開 Debugger.switch = true; Debugger.log = function(message){ try{ if(Debugger.switch){ console.log(message); } } catch(exception){ return ‘Error‘ } }
// 關閉 Debugger.switch = false; // span標簽 var timeT = document.querySelector(‘.time‘) Debugger.log(timeT) var timer; function time(){ var date = new Date(); // 獲取當前時間距離----毫秒數 var m1 = date.getTime(); Debugger.log(m1) // 設置為當前天淩晨0:0:0 date.setHours(0); date.setMinutes(0); date.setSeconds(0); // 獲取當前天淩晨距離----毫秒數 var m0 = date.getTime(); Debugger.log(m0) // 獲取距離明天的秒數 var m = 60*60*24 - (m1-m0)/1000; Debugger.log(m) timer = setInterval(time,1000); if(m<=0){ clearInterval(timer); return } timeT.innerHTML = m } setInterval(time,1000); </script> </body> </html>

js寫距離明天秒數