gpt4 book ai didi

javascript - 添加一个按钮以使用当前日期更改脚本中的值

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

我是一名初学者,我正在努力创建一个页面,该页面将显示自上一个日期以来的天数计时器。我将在工作中使用它来显示受伤后的天数。我遇到的问题是添加一个按钮,通过用当前日期更新脚本中的值来重置计时器。

这是我目前的情况

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="60" />
<title>Days Without Injury</title>
<link rel="stylesheet" href="stylesheets/styles.css">
</head>

<body>
<h1 style="font-size:100px;"><b>Days Without Injury</b></h1>

<div class="countup" id="countup1">
<span class="timeel days">00</span>
<span class="timeel timeRefDays">days</span>
</div>

<button id="reset-btn">
<img src="images/lantek.jpeg" alt="Lantek Logo" style="display:block;margin-
left:auto;margin-right:auto;padding:0;width:800px;height:300px;">;
</button>

<script>
window.onload = function() {
// Month Day, Year Hour:Minute:Second, id-of-element-container
countUpFromTime("Jul 8, 2019 12:00:00", 'countup1'); // ****** Change this line!
};

function countUpFromTime(countFrom, id) {
countFrom = new Date(countFrom).getTime();
var now = new Date(),
countFrom = new Date(countFrom),
timeDifference = (now - countFrom);

var secondsInADay = 60 * 60 * 1000 * 24,
secondsInAHour = 60 * 60 * 1000;

days = Math.floor(timeDifference / (secondsInADay) * 1);
hours = Math.floor((timeDifference % (secondsInADay)) / (secondsInAHour) * 1);
mins = Math.floor(((timeDifference % (secondsInADay)) % (secondsInAHour)) / (60 * 1000) *
1);
secs = Math.floor((((timeDifference % (secondsInADay)) % (secondsInAHour)) % (60 *
1000)) / 1000 * 1);

var idEl = document.getElementById(id);
idEl.getElementsByClassName('days')[0].innerHTML = days;
idEl.getElementsByClassName('hours')[0].innerHTML = hours;
idEl.getElementsByClassName('minutes')[0].innerHTML = mins;
idEl.getElementsByClassName('seconds')[0].innerHTML = secs;

clearTimeout(countUpFromTime.interval);
countUpFromTime.interval = setTimeout(function() {
countUpFromTime(countFrom, id);
},
1000);
}

</script>

</body>

</html>

最佳答案

更新您拥有原始时间的变量,然后再次调用脚本。下面的工作示例:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta http-equiv = "refresh" content = "60" />
<title>Days Without Injury</title>
<link rel="stylesheet" href="stylesheets/styles.css">
</head>

<body>
<h1 style="font-size:100px;"><b>Days Without Injury</b></h1>

<div class="countup" id="countup1">
<span class="timeel days">00</span>
<span class="timeel hours">00</span>
<span class="timeel minutes">00</span>
<span class="timeel seconds">00</span>
<span class="timeel timeRefDays">days</span>
</div>

<button id="reset-btn">
reset
</button>

<script>
var timeToCountUpFrom = "Jul 8, 2019 12:00:00"
window.onload = function() {
// Month Day, Year Hour:Minute:Second, id-of-element-container
countUpFromTime(timeToCountUpFrom, 'countup1'); // ****** Change this line!
document.getElementById('reset-btn').addEventListener('click', function(){
timeToCountUpFrom = new Date()
countUpFromTime(timeToCountUpFrom, 'countup1'); // ****** Change this line!
})
};
function countUpFromTime(countFrom, id) {
countFrom = new Date(countFrom).getTime();
var now = new Date(),
countFrom = new Date(countFrom),
timeDifference = (now - countFrom);

var secondsInADay = 60 * 60 * 1000 * 24,
secondsInAHour = 60 * 60 * 1000;

days = Math.floor(timeDifference / (secondsInADay) * 1);
hours = Math.floor((timeDifference % (secondsInADay)) / (secondsInAHour) * 1);
mins = Math.floor(((timeDifference % (secondsInADay)) % (secondsInAHour)) / (60 * 1000)
* 1);
secs = Math.floor((((timeDifference % (secondsInADay)) % (secondsInAHour)) % (60 *
1000)) / 1000 * 1);

var idEl = document.getElementById(id);
idEl.getElementsByClassName('days')[0].innerHTML = days;
idEl.getElementsByClassName('hours')[0].innerHTML = hours;
idEl.getElementsByClassName('minutes')[0].innerHTML = mins;
idEl.getElementsByClassName('seconds')[0].innerHTML = secs;

clearTimeout(countUpFromTime.interval);
countUpFromTime.interval = setTimeout(function(){ countUpFromTime(countFrom, id); },
1000);
}
</script>

</body>

</html>

关于javascript - 添加一个按钮以使用当前日期更改脚本中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58313958/

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