1. 程式人生 > >js頁面倒計時

js頁面倒計時

body AS ext disable utf 字符串轉換 alert click ndt

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>活動倒計時</title>
    <style>
        *
        {
            margin:0;
            padding:0;
            list-style:none;
        }
        body
        {
            font-size:18px;
            text-align:center;
        }
        .time
        {
            height:30px;
            padding:200px;
        }
        a{
            text-decoration: none;
            color: black;
            display: block;
            font-family: "宋體";
            font-style: oblique;
        }
    </style>
</head>
<body>
<div class="time">
    <span id="view"></span>
    <a id="jump" href="http://www.baidu.com">立即報名</a>
</div>
<script src="js/jquery-1.7.2.js"></script>
<script>
    $(document).ready(function(){
        //  stopFlag為定時任務對象
        var stopFlag = setInterval(function(){getDistanceTime(‘2018-06-10 21:34:30‘,‘view‘)},0);
        function getDistanceTime(time,view){
            var endTime= new Date(Date.parse(time.replace(/-/g, "/")));/*replace將時間字符串中所有的‘-‘替換成‘/‘,parse將時間格式的字符串轉換成毫秒*/
            var nowTime = new Date();
            var distance =endTime.getTime() - nowTime.getTime();/*getTime把一個date對象轉換成毫秒*/

            var day = 0;
            var hour = 0;
            var minute = 0;
            var second = 0;
            if(distance >=0){
                day = Math.floor(distance/1000/60/60/24);
                hour = Math.floor(distance/1000/60/60%24);
                minute = Math.floor(distance/1000/60%60);
                second = Math.floor(distance/1000%60);
            }else{
                stop(); // 終止定時任務
            }
            $("#view").html(day + "天" +  hour + "時" + minute + "分" + second + "秒")
        }

        function stop() {
            // 終止指定的定時任務
            window.clearInterval(stopFlag);
            //$("#jump").attr("disabled");  // 去除屬性
            $("#jump").attr("href","#").click(function () {
                alert("活動已經終止,請留意官網最新動態!");
            });
        }
    });
</script>
</body>
</html>

  

js頁面倒計時