标签:
//objElement 元素对象,strHtml 元素内的字符格式,yuTimeSpan 倒计时毫秒数,endFunc 结束处理函数
function initialTimer(objElement,strHtml,yuTimeSpan,endFunc){
if(typeof initialTimer.isIntialed === "undefined"){
//时间格式替换 objElement 元素对象,strHtml 元素内的字符格式,timespan 倒计时毫秒数,endFunc 结束处理函数
initialTimer.prototype.formateTime = function(objElement ,strHtml ,timespan ,endFunc) {
var yuTimeSpan = timespan,formatHtml = strHtml
,days = 0,hours = 0,minutes = 0,seconds = 0,milliseconds = 0;
//结束
if (timespan < 10) {
//相关操作js代码
if(typeof endFunc === "function"){endFunc();}
return true;
}
//计算出相差天数
days = Math.floor(yuTimeSpan / (24 * 3600 * 1000));
formatHtml = formatHtml.replace("{dd}",days < 10 ? ("0"+days) : days);
//计算出小时数
yuTimeSpan = yuTimeSpan % (24 * 3600 * 1000);
hours = Math.floor(yuTimeSpan / (3600 * 1000));
formatHtml = formatHtml.replace("{hh}",hours < 10 ? ("0" + hours) : hours);
//计算相差分钟数
yuTimeSpan = yuTimeSpan % (3600 * 1000);
minutes = Math.floor(yuTimeSpan / (60 * 1000));
formatHtml = formatHtml.replace("{mm}",minutes < 10 ? ("0" + minutes) : minutes);
//计算相差秒数
yuTimeSpan = yuTimeSpan % (60 * 1000);
seconds = Math.round(yuTimeSpan / 1000);
formatHtml = formatHtml.replace("{ss}",seconds < 10 ? ("0" + seconds) : seconds);
//计算相差10毫秒数
yuTimeSpan = yuTimeSpan % 1000;
milliseconds = Math.round(yuTimeSpan / 11);
formatHtml = formatHtml.replace("{SS}",milliseconds < 10 ? ("0" + milliseconds) : milliseconds);
//文本操作
objElement.innerHTML = formatHtml;
return false;
}
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;
var isStop = self.formateTime(self.objElement,self.strHtml,self.yuTimeSpan,self.endFunc);
if(isStop){
clearInterval(self.interval);
}
}, self.interTime);
}
$(function(){
//计时器
var timer = new initialTimer($("#countdownNum")[0],"{mm}:{ss}:{SS}",parseInt($("#countdownNum").attr("time")),function(){
console.log("已结束!");
});
});
标签:
原文地址:http://www.cnblogs.com/mengwei-ziyun/p/5369000.html