简体   繁体   English

倒计时片段:如果小于 1,则隐藏值

[英]Countdown Snippet: Hide Value if less than One

How can I hide the value of days and hours if less than 1?如果小于 1,如何隐藏天数小时数 Specifically, if var distance is less than 1, it should be hidden for days and hours.具体来说,如果 var distance 小于 1,则应隐藏数天和数小时。 For example: instead of 00 days 20 hours and 10 min, i'm looking for a way to just show 20 hours and 10 min.例如:我正在寻找一种只显示 20 小时 10 分钟的方法,而不是 00 天 20 小时 10 分钟。

<script type="text/javascript">
  var second = 1000,
      minute = second * 60,
      hour = minute * 60,
      day = hour * 24;
  var countDown = new Date('{{- end_date -}}').getTime(),
      x = setInterval(function() {
      var now = new Date().getTime(),
          distance = countDown - now;
      document.querySelector('.js-timer-days').innerText = Math.floor(distance / (day)),
      document.querySelector('.js-timer-hours').innerText = Math.floor((distance % (day)) / (hour)),
      document.querySelector('.js-timer-minutes').innerText = Math.floor((distance % (hour)) / (minute)),
      document.querySelector('.js-timer-seconds').innerText = Math.floor((distance % (minute)) / second);
    }, second)

</script>

All you have to do is make an if-statement.你所要做的就是做一个 if 语句。

let day_distance = Math.floor(distance / day)

if(day_distance <= 0){
    day_distance = "";
}

document.querySelector('.js-timer-days').innerText = day_distance;

Do the same thing for hours and minutes.数小时和数分钟都做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM