<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>倒计时</title>
</head>
<body>
<div id="content"></div>
<script>
function countdown(endData) {
const nowData = Date.now();
if (endData < nowData) {
throw new Error('the end time must be greater than the current time');
}
const d = endData - nowData;
let h = Math.floor(d / 1000 / 60 / 60);
let m = Math.floor(d / 1000 / 60 % 60);
let s = Math.floor(d / 1000 % 60);
h = h<=9? '0'+ h : h;
m = m<=9? '0'+ m : m;
s = s<=9? '0'+ s : s;
return `${h}:${m}:${s}`;
}
const content = document.getElementById('content');
const endData = new Date('2022/01/14 23:59:59').getTime();
setInterval(()=>{
content.innerHTML = countdown(endData);
},1000);
</script>
</body>
</html>
JS倒计时
最新推荐文章于 2024-07-17 15:26:15 发布