当它变为零时如何隐藏计数器天数?
How can i hide counter days when it gets zero?
我有一个显示 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>
感谢您的帮助
你可以检查什么时候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 ";
与小时和分钟相同
您可以使用三元运算符来检查天数是否 == 0
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>
您可以通过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>
如果您想对所有输出执行此操作并显示类似 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
我有一个显示 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>
感谢您的帮助
你可以检查什么时候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 ";
与小时和分钟相同
您可以使用三元运算符来检查天数是否 == 0
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>
您可以通过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>
如果您想对所有输出执行此操作并显示类似 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