(轉)js實現倒計時效果(年月日時分秒)


原文鏈接:http://mengqing.org/archives/js-countdown.html

之前做的活動頁面很多都用到了倒計時功能,所以整理下下次直接用。
(用的是張鑫旭寫的一個倒計時,稍作修改了下,原文:http://www.zhangxinxu.com/wordpress/?p=987)

用法:

<div class="timer">
    <span id="years">00</span>:
    <span id="months">00</span>:
    <span id="days">00</span>:
    <span id="hours">00</span>:
    <span id="minutes">00</span>:
    <span id="seconds">00</span>
</div>
  
<script type="text/javascript">
    // 獲取本機當前時間
    var mydate = new Date();
//  console.log(mydate.getFullYear(), parseInt(mydate.getMonth())+1, mydate.getDate(), mydate.getHours(), mydate.getMinutes(), mydate.getSeconds());
      
    var futureDate = eval(Date.UTC(2014, 10, 30, 12, 0, 0));
//  var nowDate = eval(Date.UTC(2013, 11, 15, 16, 36, 52));
    var nowDate = eval(Date.UTC(mydate.getFullYear(), parseInt(mydate.getMonth())+1, mydate.getDate(), mydate.getHours(), mydate.getMinutes(), mydate.getSeconds()));
    var obj = {
        sec: document.getElementById("seconds"),
        mini: document.getElementById("minutes"),
        hour: document.getElementById("hours"),
        day: document.getElementById("days"),
        day: document.getElementById("months"),
        day: document.getElementById("years")
    }
    fnTimeCountDown(futureDate, obj, nowDate, function () {
//      console.log('時間到了!');
    });
</script>

 

  JS源碼:

/* 
* 倒計時的實現
*/
var fnTimeCountDown = function (d, o, now, callback) {
    var f = {
        zero: function (n) {
            var n = parseInt(n, 10);
            if (n > 0) {
                if (n <= 9) {
                    n = "0" + n;
                }
                return String(n);
            } else {
                return "00";
            }
        },
        dv: function () {
            d = d || Date.UTC(2050, 0, 1); //如果未定義時間,則我們設定倒計時日期是2050年1月1日
            var future = new Date(d);
            var nowTime = new Date(now);
            //現在將來秒差值
            var dur = Math.round((future.getTime() - nowTime.getTime()) / 1000), pms = {
                sec: "00",
                mini: "00",
                hour: "00",
                day: "00",
                month: "00",
                year: "0"
            };
            if (dur > 0) {
                pms.sec = f.zero(dur % 60);
                pms.mini = Math.floor((dur / 60)) > 0 ? f.zero(Math.floor((dur / 60)) % 60) : "00";
                pms.hour = Math.floor((dur / 3600)) > 0 ? f.zero(Math.floor((dur / 3600)) % 24) : "00";
                //pms.day = Math.floor((dur / 86400)) > 0 ? f.zero(Math.floor((dur / 86400)) % 30) : "00";
                pms.day = Math.floor((dur / 86400)) > 0 ? f.zero(Math.floor(dur / 86400)) : "00";
                //月份,以實際平均每月秒數計算
                pms.month = Math.floor((dur / 2629744)) > 0 ? f.zero(Math.floor((dur / 2629744)) % 12) : "00";
                //年份,按回歸年365天5時48分46秒算
                pms.year = Math.floor((dur / 31556926)) > 0 ? Math.floor((dur / 31556926)) : "0";
            }
            return pms;
        },
        ui: function () {
            if (o.sec) {
                o.sec.innerHTML = f.dv().sec;
            }
            if (o.mini) {
                o.mini.innerHTML = f.dv().mini;
            }
            if (o.hour) {
                o.hour.innerHTML = f.dv().hour;
            }
            if (o.day) {
                o.day.innerHTML = f.dv().day;
            }
            if (o.month) {
                o.month.innerHTML = f.dv().month;
            }
            if (o.year) {
                o.year.innerHTML = f.dv().year;
            }
            now = now + 1000;
            if (f.dv().sec == "00" && f.dv().mini == "00" && f.dv().hour == "00" && f.dv().day == "00" && f.dv().month == "00" && f.dv().year == "0") {
                if (callback) {
                    callback();
                }
            }
 
            setTimeout(f.ui, 1000);
        }
    };
    f.ui();
};

 


免責聲明!

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



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