【问题标题】:Changing the format of jQuery Countdown?更改 jQuery 倒计时的格式?
【发布时间】:2016-04-19 16:28:15
【问题描述】:

我正试图让我的倒数计时器显示 0 Yrs, 0 Months, 7 days 0 Mins 等,但无论我尝试输入什么数字,并且尽可能多地计算出来,我都会得到 7,349 天等的答案。这是代码:

// jQuery Countdown styles 1.6.1. - plugin by Keith Wood
function counter_start() {
    var austDay = new Date();
    austDay = new Date(austDay.getFullYear() - 2016, 0 - 7, 0); // Examples: (austDay.getFullYear() + 1, 3 - 1, 6) or (2013, 3 - 1, 6)
    $("#defaultCountdown").countdown({
        until: austDay, 
        format: 'DHMS'
    });
}

我查看并阅读并询问了其他人,但这不是我的领域,我只是不明白。有人给我提个醒吗?非常感谢您的阅读。杰米。

【问题讨论】:

    标签: jquery jquery-countdown


    【解决方案1】:

    您的格式不准确。请尝试:

    $('#defaultCountdown').countdown({
        until: austDay, 
        format: 'YODM'
    });
    

    Y = 年

    O = 月

    D = 天

    M = 分钟

    【讨论】:

      【解决方案2】:

      这在您的情况下很容易使用。 在 var target_date 中指定要倒计时的日期。 您可以在脚本末尾的 countdown.innerhtml 处更改输出格式。(已经是 Y:M:D:S 格式)

      ar target_date = new Date("Aug 15, 2018").getTime();
      var days, hours, minutes, seconds;
      
      setInterval(function () {
      var countdown = document.getElementById("countdown");
      var current_date = new Date().getTime();
      var seconds_left = (target_date - current_date) / 1000;
      
      
      days = parseInt(seconds_left / 86400);
      seconds_left = seconds_left % 86400;
      
      hours = parseInt(seconds_left / 3600);
      seconds_left = seconds_left % 3600;
      
      minutes = parseInt(seconds_left / 60);
      seconds = parseInt(seconds_left % 60);
      
      countdown.innerHTML = days + "d, " + hours + "h, "+ minutes + "m, " + seconds + "s";  }, 1000);
      

      并将 html 放入正文中:

      <span id="countdown"></span> 
      

      【讨论】:

      • 我的遮阳篷出了什么问题?我认为我的遮阳篷可以提供帮助?
      【解决方案3】:

      试试这个倒数计时器插件Jquery.countdown

      通过自定义你的 custom.js 试试这个

       $('#clock').countdown('2016/10/31').on('update.countdown',   
           function(event) {
         var $this = $(this).html(event.strftime(''
      
           + '<div><span>%-d</span>day%!d</div>'
           + '<div><span>%H</span>hr</div>'
           + '<div><span>%M</span>min</div>'
           + '<div><span>%S</span>sec</div>'));
       });
      

      【讨论】:

        【解决方案4】:

        我喜欢http://keith-wood.name/countdownRef.html 官方文档中的这种方法来应用您想要的任何格式,由您自己定制:

        $(selector).countdown({ 
            until: liftoffTime, onTick: watchCountdown}); 
        
        function watchCountdown(periods) { 
            $('#monitor').text('Just ' + periods[5] + 
                ' minutes and ' + periods[6] + ' seconds to go'); 
        }
        

        在该官方文档中,他们准确地解释了 onTick 的作用以及如何使用它:

        每次倒计时更新时调用的回调函数 本身。在函数中 this 指的是持有 小部件。当前倒计时周期的数组(int[7] - 基于 格式设置)作为参数传递:[0] 是年,1 是月, [2] 是周,[3] 是天,[4] 是小时,[5] 是分钟,[6] 是 秒。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-24
          • 1970-01-01
          • 2013-01-06
          • 2020-09-04
          相关资源
          最近更新 更多