<!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{ 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>