jQuery 倒计时

(function ($) {
    jQuery.fn.extend({
        countDown: function (opts) {
            opts = jQuery.extend({
                endDate: "2013/11/11 9:12",//最后时间
                cssClass: "imgDiv",
                message: "",
                miniteToAlert: 5,
                callback: function () { return false; }
            }, opts || {});
            var $this = $(this);
            $this.addClass(opts.cssClass);
            //计时功能
            var totalSecs, days, hours, mins, secs, date;
            var date1 = new Date(opts.endDate);
            var flag = true;

            var timer = setInterval(function () {
                date = new Date();
                if (date1- date  >= 0) {
                    totalSecs = (date1-date  ) / 1000;
                    days = Math.floor(totalSecs / 3600 / 24);
                    hours = Math.floor((totalSecs - days * 24 * 3600) / 3600);
                    mins = Math.floor((totalSecs - days * 24 * 3600 - hours * 3600) / 60);
                    secs = Math.floor((totalSecs - days * 24 * 3600 - hours * 3600 - mins * 60));
                    if (flag && mins < opts.miniteToAlert && days == 0 && hours == 0) {
                        alert(opts.message);
                        flag = false;
                    }
                    if (days < 10)
                        days = "0" + days;
                    if (hours < 10)
                        hours = "0" + hours;
                    if (mins < 10)
                        mins = "0" + mins;
                    if (secs < 10)
                        secs = "0" + secs;
                    $this.html("");
                    $this.append(days + " " + hours + " " + mins + " " + secs);
                } else {
                    $this.html("");
                    $this.append("00 00 00 00");
                    opts.callback();
                    clearInterval(timer);
                }
            }, 1000);
        }
    });
})(jQuery);
posted @ 2013-11-14 08:46  任Jobs  阅读(156)  评论(0编辑  收藏  举报