【问题标题】:Reduce counter target value daily until zero每天减少计数器目标值直到零
【发布时间】:2020-03-05 14:32:00
【问题描述】:

知道如何自动将值设置为未来日期和今天日期的差值吗?我正在努力让这段代码工作。

<span class="count">200</span>
<span class="count2">200.7</span>
<span class="count" id="reducedaily">1000</span>

const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
const firstDate = new Date(2020, 10, 2);
const secondDate = new Date();
const diffDays = Math.round(Math.abs((firstDate - secondDate) / oneDay));
jQuery(#reducedaily).text(diffDays);

jQuery('.count, .count2').each(function () {
  jQuery(this).prop('Counter',0).animate({
    Counter: jQuery(this).text()
  }, {
      duration: 4000,
      easing: 'swing',
      step: function (now) {
        if(jQuery(this).hasClass('count')){
          jQuery(this).text(now.toFixed(0));
        }
        else{
          jQuery(this).text(now.toFixed(1));
        }
      }
  });
});

【问题讨论】:

  • 如何确定开始日期,然后根据当前日期计算计数?像(1000 -(2020/01/01)-今天)
  • 我认为jQuery(#reducedaily).text(diffDays) 的错误只是您的代码示例中的错字

标签: javascript jquery counter reduce


【解决方案1】:

我没有发现您的代码有任何问题 - 非 jQuery 版本似乎工作得很好(尽管出于光学考虑,我用更多小数显示日期差异)。

const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
const firstDate = new Date(2020, 10, 2);

function update() {
  const secondDate = new Date();
  const diffDays = (firstDate - secondDate) / oneDay;
  
  document.getElementById("x").innerText = (diffDays <= 0 ? "no" : diffDays.toFixed(8));
}

setInterval(update, 100);
  
Only &lt;span id="x"&gt;some&lt;/span&gt; days to go!

【讨论】:

  • 我更新了我的代码,我猜是因为计数器的原因?
猜你喜欢
  • 2022-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多