您的位置:首页 > Web前端 > JQuery

jQuery 倒计时

2013-11-14 08:46 411 查看
原文链接:http://www.cnblogs.com/rlm0909/p/3422580.html
(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);

转载于:https://www.cnblogs.com/rlm0909/p/3422580.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: