gpt4 book ai didi

javascript - 无法让小于 10 的数字在我的倒计时时钟上以两位数字显示

转载 作者:行者123 更新时间:2023-11-30 08:44:25 26 4
gpt4 key购买 nike

因此,为了在我的网站上加载倒计时时钟,我找到了一些代码来显示倒计时时钟。其实不止一个例子。我选择了一个我能理解的,并在此过程中学到了更多东西。此代码有效,但不会显示小于 10 的两位数字(即我希望 7 秒显示为 07 秒)。

    // set the date we're counting down to
var target_date = new Date("May 16, 2014").getTime();

// variables for time units
var days, hours, minutes, seconds;

// get tag element
var countdown = document.getElementById("countdown");

// update the tag with id "countdown" every 1 second
setInterval(function () {

// find the amount of "seconds" between now and target
var current_date = new Date().getTime();
var seconds_left = (target_date - current_date) / 1000;

// do some time calculations
days = parseInt(seconds_left / 86400);
seconds_left = seconds_left % 86400;

hours = parseInt(seconds_left / 3600);
seconds_left = seconds_left % 3600;

minutes = parseInt(seconds_left / 60);
seconds = parseInt(seconds_left % 60);


// format countdown string + set tag value
countdown.innerHTML = days + " DAYS " + hours + ":"
+ minutes + ":" + seconds + " UNTIL DRAWING";


}, 1000);

我尝试将时间变量设置为函数。所以我没有上面,而是:

  seconds = function () {
parseInt (seconds_left % 60);
if (seconds_left % 60 < 10) {
return "0" + seconds_left; }
else return seconds_left % 60;
}

虽然没有骰子。任何有关我可能尝试的其他方法的提示都将不胜感激。

最佳答案

如果小于 10,只需在数字开头添加一个 '0' 即可:

countdown.innerHTML = (days < 10 ? '0' : '') + days + " DAYS " +
(hours < 10 ? '0' : '') + hours + ":" +
(minutes < 10 ? '0' : '') + minutes + ":" +
(seconds < 10 ? '0' : '') + seconds + " UNTIL DRAWING";

关于javascript - 无法让小于 10 的数字在我的倒计时时钟上以两位数字显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23188515/

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