javascript實現倒計時程序


    最近在網上看到一道這樣的面試題:

  題:  網頁中實現一個計算當年還剩多少時間的倒數計時程序,要求網頁上實時動態顯示“××年還剩××天××時××分××秒”?

  我實現了,發現挺有意思,下面把我的代碼貼出來

<!doctype html>
<html  lang="en">
<head>
<meta charset="utf-8">
<script type="text/javascript">
    function dayBetween(){
        var today = new Date();
        var enday = new Date(2016,11,31,0,0,0,0);  //或 new Date("2016/12/31 0:0:0");
        var between = enday-today;
       // console.log("between"+between+"\n");
        var sec = Math.floor(between/1000);
        var day = Math.floor((Math.floor((Math.floor(sec/60))/60))/24); 
        var hours = (Math.floor((Math.floor(sec/60))/60))%24;
        var minutes = (Math.floor(sec/60))%60 ;
        var seconds =  sec%60;
       // console.log("result:"+day+"天"+hours+"時"+minutes+"分"+seconds+"秒");
       var t = document.getElementById("time");
       t.innerHTML = "離2016年12月31日0點還剩:"+"<b>"+day+""+hours+""+minutes+""+seconds+""+"</b>";
    }
    window.onload = function(){
      setInterval(dayBetween,1000);
    };
</script>
</head>
<body>
<p id="time"></p>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM