【问题标题】:JavaScript countdown timer not workingJavaScript倒数计时器不起作用
【发布时间】:2015-10-12 08:49:46
【问题描述】:

我是 JavaScript 新手。我正在使用具有以下代码的模板。它显示一个倒数计时器。我不明白为什么它不起作用。我给它提供了 90 天的价值。请指导。谢谢

// Generated by CoffeeScript 1.4.0

/*
 countdown is a simple jquery plugin for countdowns

 Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 and GPL-3.0 (http://opensource.org/licenses/GPL-3.0) licenses.

 @source: http://github.com/rendro/countdown/
 @autor: Robert Fleischmann
 @version: 1.0.1
 */

(函数(){

(函数 ($) { $.countdown = 函数(el,选项){ 变量 getDateData, _这=这; 这个.el = el; 这个.$el = $(el); this.$el.data("倒计时", this);

  this.init = function () {
    _this.options = $.extend({}, $.countdown.defaultOptions, options);
    if (_this.options.refresh) {
      _this.interval = setInterval(function () {
        return _this.render();
      }, _this.options.refresh);
    }
    _this.render();
    return _this;
  };

  getDateData = function (endDate) {
    var dateData, diff;
    endDate = Date.parse($.isPlainObject(_this.options.date) ? _this.options.date : new Date(_this.options.date));
    diff = (endDate - Date.parse(new Date)) / 1000;
    if (diff <= 0) {
      diff = 0;
      if (_this.interval) {
        _this.stop();
      }
      _this.options.onEnd.apply(_this);
    }
    dateData = {
      years: 90,
      days: 90,
      hours: 0,
      min: 0,
      sec: 90,
      millisec: 0
    };
    if (diff >= (365.25 * 86400)) {
      dateData.years = Math.floor(diff / (365.25 * 86400));
      diff -= dateData.years * 365.25 * 86400;
    }
    if (diff >= 86400) {
      dateData.days = Math.floor(diff / 86400);
      diff -= dateData.days * 86400;
    }
    if (diff >= 3600) {
      dateData.hours = Math.floor(diff / 3600);
      diff -= dateData.hours * 3600;
    }
    if (diff >= 60) {
      dateData.min = Math.floor(diff / 60);
      diff -= dateData.min * 60;
    }
    dateData.sec = diff;

    return dateData;
  };

  this.leadingZeros = function (num, length) {
    if (length == null) {
      length = 2;
    }
    num = String(num);
    while (num.length < length) {
      num = "0" + num;
    }

    return num;
  };

  this.update = function (newDate) {
    _this.options.date = newDate;

    return _this;
  };

  this.render = function () {
    _this.options.render.apply(_this, [getDateData(_this.options.date)]);

    return _this;
  };

  this.stop = function () {
    if (_this.interval) {
      clearInterval(_this.interval);
    }
    _this.interval = null;

    return _this;
  };

  this.start = function (refresh) {
    if (refresh == null) {
      refresh = _this.options.refresh || $.countdown.defaultOptions.refresh;
    }
    if (_this.interval) {
      clearInterval(_this.interval);
    }
    _this.render();
    _this.options.refresh = refresh;
    _this.interval = setInterval(function () {
      return _this.render();
    }, _this.options.refresh);

    return _this;
  };

  return this.init();
};

$.countdown.defaultOptions = {
  date: "June 7, 2087 15:03:25",
  refresh: 1000,
  onEnd: $.noop,
  render: function (date) {
    return $(this.el).html("" + date.years + " years, " + date.days + " days, " + (this.leadingZeros(date.hours)) + " hours, " + (this.leadingZeros(date.min)) + " min and " + (this.leadingZeros(date.sec)) + " sec");
  }
};

$.fn.countdown = function (options) {
  return $.each(this, function (i, el) {
    var $el;
    $el = $(el);
    if (!$el.data('countdown')) {
      return $el.data('countdown', new $.countdown(el, options));
    }
  });
};

return void 0;

})(jQuery);

}).call(this);

【问题讨论】:

  • 它不起作用怎么办?你能做一个jsfiddle例子
  • @ArunPJohny:想知道为什么它在这里不起作用:sungoldorganic.com
  • 问题是日期格式....var endDate = "December 27, 2014 15:03:25"; - 无法解析上述格式

标签: javascript


【解决方案1】:

只是改变

endDate = Date.parse($.isPlainObject(_this.options.date)
    ? _this.options.date : new Date(_this.options.date));

endDate = Date.parse($.isPlainObject(_this.options.date)
    ? _this.options.date : new Date(year,month,date));

示例:

endDate = Date.parse($.isPlainObject(_this.options.date)
    ? _this.options.date : new Date(2017,02,15));

【讨论】:

    猜你喜欢
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多