用webAPI实现新年倒计时案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
新年倒计时:<span class="time"></span>

<script>
    var time = document.querySelector('.time');
    window.onload = function () {
        var future = new Date('2021-02-11');//可以修改新年时间
        var futureTime = future.getTime();
        var timer = setInterval(function () {
            var today = new Date();
            var todayTime = today.getTime();
            var cha = futureTime - todayTime;
            var sec = parseInt(cha / 1000 % 60 + '')
            var min = parseInt(cha / 1000/60 % 60 + '')
            var hour = parseInt(cha / 1000 /60/ 60 % 24 + '')
            var date = parseInt(cha / 1000 /60/ 60 / 24 + '')
            time.innerHTML =`${date}${hour}小时${min}${sec}秒`
        },1000)
    }
</script>


</body>
</html>
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页