//objElement 元素对象,strHtml 元素内的字符格式,yuTimeSpan 倒计时毫秒数,endFunc 结束处理函数
function initialTimer(objElement,strHtml,yuTimeSpan,endFunc){
if(typeof initialTimer.isIntialed === "undefined"){
//时间格式替换 objElement 元素对象,strHtml 元素内的字符格式,timespan 倒计时毫秒数
initialTimer.prototype.formateTime = function(objElement ,strHtml ,timespan) {
var yuTimeSpan = timespan,formatHtml = strHtml
,days = 0,hours = 0,minutes = 0,seconds = 0,milliseconds = 0;
//计算出相差天数
if(formatHtml.indexOf("{dd}")>-1 || (formatHtml.indexOf("{d1}")>-1 && formatHtml.indexOf("{d2}")>-1)){
days = Math.floor(yuTimeSpan / (24 * 3600 * 1000));
formatHtml = formatHtml.replace("{dd}",days < 10 ? ("0"+days) : days);
formatHtml = formatHtml.replace("{d1}",days < 10 ? "0" : Math.floor(days/10));
formatHtml = formatHtml.replace("{d2}",days%10);
}
//计算出小时数
if(formatHtml.indexOf("{hh}")>-1 || (formatHtml.indexOf("{h1}")>-1 && formatHtml.indexOf("{h2}")>-1)){
yuTimeSpan = yuTimeSpan % (24 * 3600 * 1000);
hours = Math.floor(yuTimeSpan / (3600 * 1000));
formatHtml = formatHtml.replace("{hh}",hours < 10 ? ("0" + hours) : hours);
formatHtml = formatHtml.replace("{h1}",hours < 10 ? "0" : Math.floor(hours/10));
formatHtml = formatHtml.replace("{h2}",hours%10);
}
//计算相差分钟数
if(formatHtml.indexOf("{mm}")>-1 || (formatHtml.indexOf("{m1}")>-1 && formatHtml.indexOf("{m2}")>-1)){
yuTimeSpan = yuTimeSpan % (3600 * 1000);
minutes = Math.floor(yuTimeSpan / (60 * 1000));
formatHtml = formatHtml.replace("{mm}",minutes < 10 ? ("0" + minutes) : minutes);
formatHtml = formatHtml.replace("{m1}",minutes < 10 ? "0" : Math.floor(minutes/10));
formatHtml = formatHtml.replace("{m2}",minutes%10);
}
//计算相差秒数
if(formatHtml.indexOf("{ss}")>-1 || (formatHtml.indexOf("{s1}")>-1 && formatHtml.indexOf("{s2}")>-1)){
yuTimeSpan = yuTimeSpan % (60 * 1000);
seconds = Math.round(yuTimeSpan / 1000);
formatHtml = formatHtml.replace("{ss}",seconds < 10 ? ("0" + seconds) : seconds);
formatHtml = formatHtml.replace("{s1}",seconds < 10 ? "0" : Math.floor(seconds/10));
formatHtml = formatHtml.replace("{s2}",seconds%10);
}
//计算相差10毫秒数
if(formatHtml.indexOf("{SS}")>-1 || (formatHtml.indexOf("{S1}")>-1 && formatHtml.indexOf("{S2}")>-1)){
yuTimeSpan = yuTimeSpan % 1000;
milliseconds = Math.round(yuTimeSpan / 11);
formatHtml = formatHtml.replace("{SS}",milliseconds < 10 ? ("0" + milliseconds) : milliseconds);
formatHtml = formatHtml.replace("{S1}",milliseconds < 10 ? "0" : Math.floor(milliseconds/10));
formatHtml = formatHtml.replace("{S2}",milliseconds%10);
}
//文本操作
objElement.innerHTML = formatHtml;
}
initialTimer.prototype.isIntialed = true;
}
//倒计时计时器
var self = this;
this.objElement = objElement;
this.strHtml = strHtml;
this.yuTimeSpan = yuTimeSpan;
this.endFunc = endFunc;
this.interTime = 60;
this.interval = setInterval(function(){
self.yuTimeSpan = self.yuTimeSpan - self.interTime;
self.formateTime(self.objElement,self.strHtml,self.yuTimeSpan);
if (self.yuTimeSpan <= self.interTime) {
setTimeout(function(){
//结束
if(typeof self.endFunc === "function"){self.endFunc();}
self.formateTime(self.objElement,self.strHtml,0);
},self.yuTimeSpan);
clearInterval(self.interval);
}
}, self.interTime);
}
//计时器
//var timer = new initialTimer($("#countdownNum")[0],"{mm}:{ss}:{SS}",parseInt($("#countdownNum").attr("time")),function(){
// console.log("已结束!");
//});