javascript - 如何减少下面代码中的时间直到它变为零?
问题描述
我试图通过以毫秒为单位给出时间和日期来进行时间戳倒计时,但我无法解决这个问题。
var distance = countDownDate--
只会将数字递减一次。我想像计时器一样将它递减到零。
// Set the date we're counting down to
var d = new Date();
d.setTime(405000000);
var countDownDate = d;
// Update the count down every 1 second
var x = setInterval(function() {
//this line decrementing only by one time, but I need till the time becomes zero
var distance = countDownDate--;
// 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);
p {
text-align: center;
font-size: 60px;
margin-top: 0px;
}
<p id="demo"></p>
解决方案
您的代码确实有效。
问题是,difference
以毫秒为单位,你每秒递减一。因此,如果您停留 1000 秒(大约 17 分钟),您会看到计数器减少 1 秒。
1000
为了解决它,在每次迭代中减去:
// Set the date we're counting down to
var d = new Date();
d.setTime(405000000);
var countDownDate = d;
// Update the count down every 1 second
var x = setInterval(function() {
//this line deceremneting only by one time, but I need till the time becomes zero
var distance = countDownDate -= 1000;
// 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);
p {
text-align: center;
font-size: 60px;
margin-top: 0px;
}
<p id="demo"></p>
但是,setTimeout
并且setInterval
可能不准确,因此建议每次迭代都获取当前时间:
// Set the date we're counting down to
var endDate = Date.now() + 405000000;
// Update the count down every 1 second
var x = setInterval(function() {
//this line deceremneting only by one time, but I need till the time becomes zero
var distance = endDate - Date.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);
p {
text-align: center;
font-size: 60px;
margin-top: 0px;
}
<p id="demo"></p>
推荐阅读
- java - Spring Boot 应用程序在运行时卡住了
- java - 如何封装 Java Streams 操作
- javascript - 从 JSON 有效负载中的特定 JSON 对象中删除特殊字符
- amazon-web-services - 使用 lambda webhook 触发 AWS 代码管道
- delphi - 如何在 TMSFMXGrid (在 Firemonkey 中)中使所有单元格只读?
- php - 如何根据第一个选项显示选择选项值
- multi-tenant - 为多租户应用程序设计 UI
- elasticsearch - 执行关键字搜索的正确方法是什么?
- flutter - Flutter 如何在 SliverChildBuilderDelegate 中的容器之间留出空隙
- r - 在没有循环的矩阵上比较数组