javascript 簡單時間倒計時。

來源:互聯網
上載者:User

標籤:art   return   title   function   html   http   開始   doc   nbsp   

給一個新人講解時隨手寫的demo~

<!DOCTYPE html><html><head>    <meta charset="UTF-8" />    <title>倒計時</title>    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>    <script>        function getTimeStr(time)        {            var d=Math.floor(time/86400);            time=time-d*86400;            var h=Math.floor(time/3600);            time=time-h*3600;            var m=Math.floor(time/60);            time=time-m*60;            var s=time            if (h<10) h="0"+h;            if (m<10) m="0"+m;            if (s<10) s="0"+s;            return d+"天"+h+":"+m+":"+s        }        var leftTime=0;        var timer=null;        function timeStep()        {            leftTime=leftTime-1;            $("#leftTime").html(getTimeStr(leftTime));            if (leftTime==0)            {                alert("時間到拉。")                clearTimeout(timer)                timer=null;            }            else            {                timer=setTimeout(timeStep,1000);            }        }        $(function(){            $(document).on("click","#beginBtn",function(){                var beginTime=$("#time").val();                if (beginTime==""){                    beginTime=0;                }                leftTime=beginTime;                $("#leftTime").html(getTimeStr(leftTime));                if (timer||beginTime==0) return                timer=setTimeout(timeStep,1000)            });        });    </script></head><body>        <span id="leftTime" style="min-height: 50px;line-height:50px;display:block;">00:00:00</span>    <input type="number" id="time" / >    <button id="beginBtn">開始倒計時</button></body></html>

 

javascript 簡單時間倒計時。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.