js实现简单的倒计时功能
以下是一个简单的JavaScript倒计时功能实现示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>倒计时示例</title>
<script>
function countdown() {
var endTime = new Date("December 31, 2023 23:59:59").getTime();
var now = new Date().getTime();
var timeLeft = endTime - now;
var days = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
var hours = Math.floor((timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((timeLeft % (1000 * 60)) / 1000);
document.getElementById("countdown").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
if (timeLeft < 0) {
clearInterval(interval);
document.getElementById("countdown").innerHTML = "EXPIRED";
}
}
var interval = setInterval(countdown, 1000);
</script>
</head>
<body>
<div id="countdown"></div>
</body>
</html>
这段代码会在网页上显示一个倒计时,直至2023年12月31日23时59分59秒过期。你可以根据需要修改endTime
变量来设置倒计时的结束时间。
评论已关闭