首页 🍋Code,🎉收集

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>倒计时(天数,时,分,秒)</title>
</head>
<body>
        <button type="button"
                        onclick="document.getElementById('demo').innerHTML = Date()">点这里显示实时时间</button>
    <p id="demo"></p>
<h4>距离高考还有:</h4>
<h3 style="color:red">
<div id="timer"></div>
<script language="javascript" type="text/javascript">
    timer('2022-06-07 12:00:00','day');
 
    /**
     * 入参第一个为时间字符串,第二个参数为模式选择,如果传入'day',按天数倒计时到秒,不传值,按小时精确到秒
     * @param timeStr
     * @param item
     */
    function timer(timeStr,item){
        setInterval(function(){
            let nowTime = new Date(timeStr) - new Date;
            let minutes = parseInt(nowTime / 1000 / 60 % 60, 10);//计算剩余的分钟
            let seconds = parseInt(nowTime / 1000 % 60, 10);//计算剩余的秒数
 
 
            minutes = checkTime(minutes);
            seconds = checkTime(seconds);
            if(item==='day'){
                let days = parseInt(nowTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
                let hours = parseInt(nowTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
                days = checkTime(days);
                hours = checkTime(hours);
                document.getElementById('timer').innerHTML=days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
            }else{
                let hours = parseInt(nowTime / ( 1000 * 60 * 60), 10); //计算剩余的小时
                hours = checkTime(hours);
                document.getElementById("timer").innerHTML = hours + "小时" + minutes + "分" + seconds + "秒";
            }
        },1000);
    }
 
    function checkTime(i) { //将0-9的数字前面加上0,例1变为01
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }
</script>
    </h3>
<a href="#">在奋斗的过程中,常会遭受挫折,惟有坚持到底,才会获得最后的成功!</a>    
</body>
</html>



文章评论

目录