jQuery.fn.extend({ countdown: function(options) { options = options || {}; options.end = Number(options.end) || ""; options.interval = options.interval || 1000; options.ontimeout = options.ontimeout || function() { }; options.global = options.global || 0; options.format = options.format || 'm'; //可以是Time,或者Minute options.inputType = options.inputType || 'm'; var FORMATID = "format"; this.data(FORMATID, options.format); this.data('ss', options.ontimeout); jQuery.countdowInterval = options.interval; jQuery.onCountdown = function(container, count) { container.html(jQuery.timeTranform(count <= 0 ? 0 : count, container.data(FORMATID))); if (count <= 0) container.data('ss')(); //options.ontimeout(); } if (isNaN(options.end)) return; var count = jQuery.fromTimes(options.end, options.inputType); return this.each(function() { var me = $(this); me.data(jQuery.STOPPOINTID, 0); jQuery.stopwatch(me, count); }); }, calcTimer: function(options) { options = options || {}; options.limit = options.limit || ""; options.interval = options.interval || 1000; options.ontimeout = options.ontimeout || function() { }; options.onCountdownError = options.onCountdownError || function() { }; options.format = options.format || 's'; //可以是Time,或者Minute var FORMATID = "format"; this.data(FORMATID, options.format); this.data('ss', options.ontimeout); jQuery.countdowInterval = options.interval; jQuery.onCountdownErr = function(container) { options.onCountdownError(container); } jQuery.onCountdown = function(container, count) { container.html(jQuery.timeTranform(count <= 0 ? 0 : count, container.data(FORMATID))); if (count <= 0) container.data('ss')(); //options.ontimeout(); } if (isNaN(options.limit)) return; return this.each(function() { var me = $(this); jQuery.calcSecond(me, options.limit); }); }, addminute: function(minute) { var sec = jQuery.fromMinutes(minute); return this.each(function() { var me = $(this); jQuery.stopwatch(me, sec); }); }, gettime: function() { return this.data(jQuery.STOPPOINTID); }, clear: function() { if (this.data(jQuery.INTERVALID)) window.clearInterval(this.data(jQuery.INTERVALID)); } }); jQuery.extend({ INTERVALID: "intervalID", STOPPOINTID: "stopPoint", LASTPOINTID: "lastPoint", countdowInterval: 1000, onCountdown: function(container, count) { }, onCountdownErr: function(container) { }, calcSecond: function(container, sec) { var me = this; var intervalID = container.data(me.INTERVALID); if (intervalID) window.clearInterval(intervalID); var now = new Date(); // 根据传入参数计算结束时间 var stopPoint = now.addSeconds(sec); container.data(me.STOPPOINTID, stopPoint); // 如果当前时间已经大于结束时间,则返回0标识结束 if (stopPoint <= now) { me.onCountdown(container, 0); } else { // 间隔指定时间倒计时 intervalID = window.setInterval(function() { var endPoint = container.data(me.STOPPOINTID); var curDate = new Date(); if (curDate >= endPoint) { window.clearInterval(container.data(me.INTERVALID)); me.onCountdown(container, 0); } else { var point = curDate.dateDiff('s', endPoint); // 检查上次时间是否小于本次时间,若是,表示用户修改了本地计算机时间 var lastPoint = container.data(me.LASTPOINTID); if (lastPoint && lastPoint < point) me.onCountdownErr(container); container.data(me.LASTPOINTID, point); me.onCountdown(container, point); } }, me.countdowInterval); container.data(me.INTERVALID, intervalID); } }, stopwatch: function(container, limit) { var me = this; // 如果已经开始计时,则清除后重新计算 var intervalID = container.data(me.INTERVALID); if (intervalID != null) { window.clearInterval(intervalID); } // 计算总计数,并累加至内存中 var stopPoint = container.data(me.STOPPOINTID); if (stopPoint == null) stopPoint = 0; stopPoint = stopPoint + limit; container.data(me.STOPPOINTID, stopPoint); if (stopPoint <= 0) { //stopPoint = 0; me.onCountdown(container, stopPoint); return; } // 开始倒计时,当到达0时停止 intervalID = window.setInterval(function() { stopPoint = stopPoint - 1; if (Number(stopPoint) <= 0) { window.clearInterval(container.data(me.INTERVALID)); } container.data(me.STOPPOINTID, stopPoint); me.onCountdown(container, stopPoint); }, me.countdowInterval); container.data(me.INTERVALID, intervalID); }, timeTranform: function(count, format) { if (format == null) return count; var unit = format.toLowerCase(); if (unit == "h" || unit == "hour") return Math.floor(count / 3600); if (unit == "m" || unit == "minute") return Math.floor(count / 60); if (unit == "s" || unit == "second") return count; return this.timeFormatTranformer(count, format); }, timeFormatTranformer: function(count, format) { var hours = Math.floor(count / 3600); var minute = Math.floor(count % 3600 / 60); var second = Math.floor(count % 3600 % 60); return format.replace(/#h/i, hours).replace(/#m/i, minute).replace(/#s/i, second); }, fromTimes: function(count, format) { if (/^s$/i.test(format)) return count; if (/^m$/i.test(format)) return this.fromMinutes(count); if (/^h$/i.test(format)) return this.fromHours(count); }, fromMinutes: function(minute) { return minute * 60; }, fromHours: function(hours) { return hours * 3600; } }); Date.prototype.add = function (milliseconds) { var m = this.getTime() + milliseconds; return new Date(m); }; Date.prototype.addSeconds = function (second) { return this.add(second * 1000); }; Date.prototype.addMinutes = function (minute) { return this.addSeconds(minute * 60); }; Date.prototype.addHours = function (hour) { return this.addMinutes(60 * hour); }; Date.prototype.dateDiff = function (interval, objDate2) { var d = this, i = {}, t = d.getTime(), t2 = objDate2.getTime(); i['y'] = objDate2.getFullYear() - d.getFullYear(); i['q'] = i['y'] * 4 + Math.floor(objDate2.getMonth() / 4) - Math.floor(d.getMonth() / 4); i['m'] = i['y'] * 12 + objDate2.getMonth() - d.getMonth(); i['ms'] = objDate2.getTime() - d.getTime(); i['w'] = Math.floor((t2 + 345600000) / (604800000)) - Math.floor((t + 345600000) / (604800000)); i['d'] = Math.floor(t2 / 86400000) - Math.floor(t / 86400000); i['h'] = Math.floor(t2 / 3600000) - Math.floor(t / 3600000); i['n'] = Math.floor(t2 / 60000) - Math.floor(t / 60000); i['s'] = Math.floor(t2 / 1000) - Math.floor(t / 1000); return i[interval]; }; Date.prototype.Format = function (formatStr) { var str = formatStr; var Week = ['日', '一', '二', '三', '四', '五', '六']; str = str.replace(/yyyy|YYYY/, this.getFullYear()); str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100)); var month = this.getMonth() + 1; str = str.replace(/MM/, month > 9 ? month.toString() : '0' + month); str = str.replace(/M/g, month); str = str.replace(/w|W/g, Week[this.getDay()]); str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate()); str = str.replace(/d|D/g, this.getDate()); str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours()); str = str.replace(/h|H/g, this.getHours()); str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes()); str = str.replace(/m/g, this.getMinutes()); str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds()); str = str.replace(/s|S/g, this.getSeconds()); return str; }