- /*
- var countdown = new CountDown(
- document.getElementById('countdown_wrapper'),
- new Date(2015, 8, 27, 0, 0)
- );
- countdown.run();
- */
- var CountDown = function(wrapper, endDate) {
- // init
- this.wrapper = wrapper;
- this.timerRunning = false;
- this.endDate = endDate;
- this.template = '{days} DAYS {hours} HOURS {mins} MINS {secs} SECS';
- }
- CountDown.prototype.showtime = function(){
- var now = new Date();
- var leftTime = this.endDate.getTime() - now.getTime();
- var leftsecond = parseInt(leftTime / 1000);
- var day1 = Math.floor(leftsecond / (60 * 60 * 24));
- var hour1 = Math.floor((leftsecond - day1 * 24 * 60 * 60) / 3600);
- var hour = Math.floor((leftsecond - 60 * 60) / 3600);
- if (hour < 0) {
- hour = 0;
- }
- if (day1 < 0) {
- hour = hour1
- }
- var minute = Math.floor((leftsecond - day1 * 24 * 60 * 60 - hour1 * 3600) / 60);
- var second = Math.floor(leftsecond - day1 * 24 * 60 * 60 - hour1 * 3600 - minute * 60);
- var html = '';
- if (leftTime > 0) {
- html = this.template;
- html = html.replace('{days}', day1);
- html = html.replace('{hours}', hour1);
- html = html.replace('{mins}', minute);
- html = html.replace('{secs}', second);
- this.wrapper.innerHTML = html;
- } else {
- html = this.template;
- html = html.replace('{days}', 0);
- html = html.replace('{hours}', 0);
- html = html.replace('{mins}', 0);
- html = html.replace('{secs}', 0);
- this.wrapper.innerHTML = html;
- }
- this.timerRunning = true;
- }
- CountDown.prototype.run = function(){
- var _this = this;
- CountDown_timer = setTimeout(function() {
- _this.showtime();
- CountDown_timer = setTimeout(arguments.callee, 1000);
- _this.timerRunning = true;
- }, 1000);
- }
- CountDown.prototype.stop = function(){
- if(timerRunning)
- clearTimeout(CountDown_timer);
- this.timerRunning = false;
- }
- CountDown.prototype.constructor = CountDown;
来源: http://www.phpxs.com/code/1003747/