gpt4 book ai didi

javascript - 带天、周、月和年的秒表/倒数计时器

转载 作者:行者123 更新时间:2023-11-30 19:34:27 25 4
gpt4 key购买 nike

我正在尝试在 javascript 中为我的网站找到一个秒表/计时计时器,其中包括天、周、月和年。

我已经尝试找到这个,但我找到的所有东西都只有几分钟和几秒钟。

我发现我喜欢的一段代码是这样的:

var minutesLabel = document.getElementById("minutes");
var secondsLabel = document.getElementById("seconds");
var totalSeconds = 0;
setInterval(setTime, 1000);

function setTime() {
++totalSeconds;
secondsLabel.innerHTML = pad(totalSeconds % 60);
minutesLabel.innerHTML = pad(parseInt(totalSeconds / 60));
}

function pad(val) {
var valString = val + "";
if (valString.length < 2) {
return "0" + valString;
} else {
return valString;
}
}
<label id="minutes">00</label>:<label id="seconds">00</label>

来自这个问题:plain count up timer in javascript ,但这同样没有天、周、月和年。

有人可以将其重写为有天、周、月和年吗?

另外,这不是重复的。

最佳答案

请注意,这只是一个简单的例子,并不是一个确切的例子:

var minutesLabel = document.getElementById("minutes");
var secondsLabel = document.getElementById("seconds");
var hours = document.getElementById("hours");
var days = document.getElementById("days");
var weeks = document.getElementById("weeks");
var months = document.getElementById("months");
var years = document.getElementById("years");
var totalSeconds =31540000;
setInterval(setTime, 1000);

function setTime() {
--totalSeconds;
var yearD = parseInt(totalSeconds / (31540000))
var yearM = parseInt(totalSeconds % (31540000))

var monthD = parseInt(yearM / (2628336.2137829))
var monthM = parseInt(yearM % (2628336.2137829))

var weekD = parseInt(monthM / (604876.71234816079959))
var weekM = parseInt(monthM % (604876.71234816079959))

var dayD = parseInt(weekM / (86410.958906880114228))
var dayM = parseInt(weekM % (86410.958906880114228))

var hourD = parseInt(dayM / (3600))
var hourM = parseInt(dayM % (3600))

var minD = parseInt(hourM / (60))
var secD = parseInt(hourM % (60))



secondsLabel.innerHTML = pad(secD);
minutesLabel.innerHTML = pad(minD);
hours.innerHTML = pad(hourD);
days.innerHTML = pad(dayD);
weeks.innerHTML = pad(weekD);
months.innerHTML = pad(monthD);
years.innerHTML = pad(yearD);
}

function pad(val) {
var valString = val + "";
if (valString.length < 2) {
return "0" + valString;
} else {
return valString;
}
}
years<label id="years">00:</label>
months<label id="months">00:</label>
weeks<label id="weeks">00:</label>
days<label id="days">00:</label>
hours<label id="hours">00</label>
<label id="minutes">00</label>:<label id="seconds">00</label>

关于javascript - 带天、周、月和年的秒表/倒数计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56092577/

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