我有一个显示0的计数器,我想在它变为0时隐藏它。 因此,如果计数器显示(0天6小时25分钟)我希望它(6小时25分钟),只需将其隐藏在0 ... 这是我的代码:
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
text-align: center;
font-size: 60px;
margin-top:0px;
}
</style>
</head>
<body>
<p id="demo"></p>
<script>
// Set the date we're counting down to
var countDownDate = new Date("April 12, 2018 15:37:25").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
</body>
</html>
感谢您的帮助
答案 0 :(得分:2)
您可以查看days === 0
时间并将其填空
days = days === 0 ? '' : days + "d "
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = days + hours + "h "
+ minutes + "m " + seconds + "s ";
相同的小时和分钟
答案 1 :(得分:2)
如果天== 0
,您可以使用三元运算符chceck document.getElementById("demo").innerHTML = ((days) ? days + "d " : '') + hours + "h " + minutes + "m " + seconds + "s ";
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
p {
text-align: center;
font-size: 60px;
margin-top:0px;
}
</style>
</head>
<body>
<p id="demo"></p>
<script>
// Set the date we're counting down to
var countDownDate = new Date("April 12, 2018 15:37:25").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = ((days) ? days + "d " : '') + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
</body>
</html>
答案 2 :(得分:2)
如果您想对所有输出执行此操作并将1d 0h 52m 0s
显示为1d 52m
,则逻辑应为:
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML =
((!days) ? '' : days + "d ") +
((!days && !hours) ? '' : hours + "h ") +
((!days && !hours && !minutes) ? '' : minutes + "m ") +
((!days && !hours && !minutes && !seconds) ? '' : seconds + "s ";
这样的时间就像0d 0h 52m 0s
52m 0s
答案 3 :(得分:1)
您可以通过document.getElementById("demo").innerHTML
更改时间来设置条件,
(days === 0 ? '' : days + "d ") +
(hours === 0 ? '' : hours + "h ")+
(minutes === 0 ? '' : minutes + "m ")+
seconds + "s ";
这将检查days
,hours
和minutes
的零值,并在HTML中指定null
,
// Set the date we're counting down to
var countDownDate = new Date("April 12, 2018 15:37:25").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML =
(days === 0 ? '' : days + "d ") +
(hours === 0 ? '' : hours + "h ")+
(minutes === 0 ? '' : minutes + "m ")+
seconds + "s ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
p {
text-align: center;
font-size: 60px;
margin-top:0px;
}
<p id="demo"></p>