一段倒計時的程式碼(JS)
阿新 • • 發佈:2019-02-11
function Timer(startTime, endTime) {
this.startDate = new Date(startTime);
this.endDate = new Date(endTime);
this.startTime = this.startDate.getTime();
this.endTime = this.endDate.getTime() - this.startTime;
this.countDownTime = this.endTime;
}
Timer.prototype.start = function () {
this.countDown();
};
Timer.prototype.countDown = function () {
this.countDownTime -= 1000;
var time = new Date(this.countDownTime), that = this;
$('.hour').text(time.getUTCHours());
$('.minute').text(time.getUTCMinutes());
$('.second').text(time.getUTCSeconds());
this.timer = setTimeout(function () {
that.countDown.call(that);
}, 1000);
if (this.countDownTime <= 0) {
clearTimeout(this.timer);
}
};
var a = new Timer('2015-04-21 16:43:20', '2015-04-21 16:43:22');
a.start();
this.startDate = new Date(startTime);
this.endDate = new Date(endTime);
this.startTime = this.startDate.getTime();
this.endTime = this.endDate.getTime() - this.startTime;
this.countDownTime = this.endTime;
}
Timer.prototype.start = function () {
this.countDown();
};
Timer.prototype.countDown = function () {
this.countDownTime -= 1000;
var time = new Date(this.countDownTime), that = this;
$('.hour').text(time.getUTCHours());
$('.minute').text(time.getUTCMinutes());
$('.second').text(time.getUTCSeconds());
this.timer = setTimeout(function () {
that.countDown.call(that);
}, 1000);
if (this.countDownTime <= 0) {
clearTimeout(this.timer);
}
};
var a = new Timer('2015-04-21 16:43:20', '2015-04-21 16:43:22');
a.start();