简体   繁体   中英

Countdown Snippet: Hide Value if less than One

How can I hide the value of days and hours if less than 1? Specifically, if var distance is less than 1, it should be hidden for days and hours. 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.

<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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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