日期:2014-05-16 浏览次数:20392 次
DateTimer = function(elem_id, current) {
var local = new Date().getTime();
this.current = current;
this.diff = current - local;
this.elem = document.getElementById(elem_id);
}
DateTimer.prototype.start = function() {
var _self = this;
var timerID = setTimeout(function() {
_self.start()
}, 1000);
if (this.elem == null)
return;
if (this.elem == null)
return;
var value = new Date().getTime() + this.diff;
var d = new Date(value);
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
if (hours < 10)
hours = '0' + hours;
if (minutes < 10)
minutes = '0' + minutes;
if (seconds < 10)
seconds = '0' + seconds;
var innerstr = hours + ':' + minutes + ':' + seconds;
this.elem.innerHTML = innerstr;
}
DateTimer.prototype.showTime = function() {
if (this.elem.size() == 0)
return;
var ms = this.current;
var s = h = m = 0;
s = Math.floor(ms / 1000);
if (s >= 60) {
m = Math.floor(s / 60);
s = s - m * 60
}
if (m >= 60) {
h = Math.floor(m / 60);
m = m - h * 60
}
if (s < 10)
s = "0" + s;
if (m < 10)
m = "0" + m;
if (h < 10)
h = "0" + h;
this.elem.each(function() {
this.innerHTML = h + ":" + m + ":" + s;
});
}
Timer = function(elem_id, current_ms, callback, showType) {
this.current_ms = current_ms;
this.elem = jQuery('[id=' + elem_id + ']');
this.count = 0;
this.delay = false;
this.callback = callback;
this.timerID = 0;
this.showType = showType;
console.info(this.elem);
}
Timer.prototype.start = function() {
var _self = this;
this.timerID = setTimeout(function() {
_self.start()
}, 1000);
if (this.elem.size() == 0)
return;
if (this.showType == "itemTimer") {
this.showTime2();
} else {
this.showTime();
}
this.current_ms -= 1000;
if (this.current_ms <= 0) {
if (this.showType == "show2") {
this.elem.each(function() {
// this.innerHTML = "还剩:0小时0分钟0秒";
this.innerHTML = "已结束";
});
} else {
this.elem.innerHTML = "00:00:00";
}
if (this.callback && typeof this.callback == 'function') {
try {
this.callback();
} catch (err) {
}
}
return;
}
}
Timer.prototype.showTime = function() {
if (this.elem.size() == 0)
return;
var ms = this.current_ms;
if (ms <= 0) {
this.innerHTML = "已结束";
this.clear();
return;
}
var s = h = m = d = 0;
s = Math.floor(ms / 1000);
if (s >= 60) {
m = Math.floor(s / 60);
s = s - m * 60;
}
if (m >= 60) {
h = Math.floor(m / 60);
m = m - h * 60;
}
if (h >= 24) {
d = Math.floor(h / 24);
h = h - d * 24;
}
this.elem.each(function() {
this.innerHTML = "剩余时间:" + (d > 0 ? d + "天" : "") + h + ":" + m
+ ":" + s + "";
});
}
Timer.prototype.showTime2 = function() {
if (this.elem.size() == 0)
return;
var ms = this.current_ms;
if (ms <= 0) {
this.innerHTML = "已结束";
this.clear();
return;
}
var s = h = m = d = 0;
s = Math.floor(ms / 1000);
if (s >= 60) {
m = Math.floor(s / 60);
s = s - m * 60;
}
if (m >= 60) {
h = Math.floor(m / 60);
m = m - h * 60;
}
if (h >= 24) {
d = Math.floor(h / 24);
h = h - d * 24;
}
this.elem.each(function() {
this.innerHTML = "(剩余:<b>" + (d > 0 ? d + "</b>天<b>" : "") + h
+ "</b>小时<b>" + m + "</b>分<b>" + s + "</b>秒)";
});
}
Timer.prototype.clear = function() {
window.clearTimeout(this.timerID);
this.current_ms = 0;
}
function startTimer(id, time, callback, showType) {
if (time < 0)
return null;
var timer = new Timer(id, time, callback, showType);