gpt4 book ai didi

javascript - 使用jquery倒计时

转载 作者:行者123 更新时间:2023-12-02 23:33:49 25 4
gpt4 key购买 nike

我正在倒计时,实际上工作正常,现在我想从 0-9 添加 0每个数字我想附加 0。现在它带有一个数字,例如 6 小时,我想是 06 小时。谁能给我推荐一下。

const second = 1000,
minute = second * 60,
hour = minute * 60,
day = hour * 24;

let countDown = new Date('July 17, 2019 00:00:00').getTime(),
x = setInterval(function() {

let now = new Date().getTime(),
distance = countDown - now;

document.getElementById('days').innerText = Math.floor(distance / (day)),
document.getElementById('hours').innerText = Math.floor((distance % (day)) / (hour)),
document.getElementById('minutes').innerText = Math.floor((distance % (hour)) / (minute)),
document.getElementById('seconds').innerText = Math.floor((distance % (minute)) / second);
}, second)
if (second < 10 && minute < 10 && hour < 10 && day < 10) {
countDown = "0" + countDown;
}
.popup-counter {
display: flex;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<div class="popup-counter">
<div class="counter-timings">
<div class="counter" id="days"></div>
<h4>Days</h4>
</div>
<div class="counter-timings">
<div class="counter" id="hours"></div>
<h4>Hours</h4>
</div>
<div class="counter-timings">
<div class="counter" id="minutes"></div>
<h4>Minutes</h4>
</div>
<div class="counter-timings">
<div class="counter" id="seconds"></div>
<h4>Seconds</h4>
</div>
</div>

最佳答案

尝试这样。

将值分配给变量并检查是否小于 10,然后添加一个零,否则保持原样。

(hours < 10)?'0'+hours:hours,

const second = 1000,
minute = second * 60,
hour = minute * 60,
day = hour * 24;

let countDown = new Date('July 17, 2019 00:00:00').getTime(),
x = setInterval(function() {

let now = new Date().getTime(),
distance = countDown - now;
var hours = Math.floor((distance % (day)) / (hour));
var days = Math.floor(distance / (day));
var minutes = Math.floor((distance % (hour)) / (minute));
var seconds = Math.floor((distance % (minute)) / second);
document.getElementById('days').innerText = (days < 10 )?'0'+days:days,
document.getElementById('hours').innerText = (hours < 10)?'0'+hours:hours,
document.getElementById('minutes').innerText = (minutes < 10)?'0'+minutes:minutes,
document.getElementById('seconds').innerText = (seconds < 10)?'0'+seconds:seconds;
}, second)
if (second < 10 && minute < 10 && hour < 10 && day < 10) {
countDown = "0" + countDown;
}
.popup-counter {
display: flex;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<div class="popup-counter">
<div class="counter-timings">
<div class="counter" id="days"></div>
<h4>Days</h4>
</div>
<div class="counter-timings">
<div class="counter" id="hours"></div>
<h4>Hours</h4>
</div>
<div class="counter-timings">
<div class="counter" id="minutes"></div>
<h4>Minutes</h4>
</div>
<div class="counter-timings">
<div class="counter" id="seconds"></div>
<h4>Seconds</h4>
</div>
</div>

关于javascript - 使用jquery倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56375676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com