倒计时为什么不再移动?

huangapple go评论71阅读模式
英文:

Why is the countdown no longer moving?

问题

我目前正在为我的男朋友建立一个网站。我复制了一个倒计时的代码,之前它是可以工作的,但是我决定重新制作网站后,这个代码不再工作了。我该怎么办!

以下是JS代码的翻译部分:

window.addEventListener('load', (event) => {
console.log('页面已完全加载');
countUpFromTime("July 9, 2021 12:00:00", 'countup1');
});
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);
};

这是HTML的翻译部分:

<script type="text/javascript" src="time.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
<link href="styles2.css" rel="stylesheet">
<link href="transitions.css" rel="stylesheet">
<link href="fade.css" rel="stylesheet">
<meta http-equiv="refresh" content="35; url = gallery.html" />

<div class="h-100 d-flex align-items-center justify-content-center">
<div>
<h3 id="one">那么...过去多长时间了?</h3>
<br>
<h3 id="two"><b>让我们看看吧!</b></h2>
<br><br>

<h3>
<div id="three" class="countup" id="countup1">
<span class="sep">
<span class="timeel days">00</span>
<br>
<span class="timeel timeRefDays">天</span>
</span>
<span class="sep">
<span class="timeel hours">00</span>
<br>
<span class="timeel timeRefHours">小时</span>
</span>
<span class="sep">
<span class="timeel minutes">00</span>
<br>
<span class="timeel timeRefMinutes">分钟</span>
</span>
<span class="sep">
<span class="timeel seconds">00</span>
<br>
<span class="timeel timeRefSeconds">秒钟</span>
</span>
</div>
</h3>

</div>
</div>
</div>

这是CSS的翻译部分:

.countup .sep {
display: inline-block;
padding: 50px;
background: rgba(0, 128, 128, 0.707);
margin: 0;
color: rgb(0, 0, 0);
min-width: 2.6rem;
border-radius: 10px;
}

如果需要进一步的帮助,请告诉我。

英文:

I'm currently building a site for my boyfriend. I copied code for a countdown and was working previously, however, I decided to re-make the site and the code is no longer working. What do I do!

Here is the JS.

window.addEventListener(&#39;load&#39;, (event) =&gt; {
console.log(&#39;The page has fully loaded&#39;);
countUpFromTime(&quot;July 9, 2021 12:00:00&quot;, &#39;countup1&#39;);
});
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(&#39;days&#39;)[0].innerHTML = days;
idEl.getElementsByClassName(&#39;hours&#39;)[0].innerHTML = hours;
idEl.getElementsByClassName(&#39;minutes&#39;)[0].innerHTML = mins;
idEl.getElementsByClassName(&#39;seconds&#39;)[0].innerHTML = secs;
clearTimeout(countUpFromTime.interval);
countUpFromTime.interval = setTimeout(function () { countUpFromTime(countFrom, id); }, 1000);
};

Here is the HTML.

&lt;script type=&quot;text/javascript&quot; src=&quot;time.js&quot;&gt;&lt;/script&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;
integrity=&quot;sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ&quot; crossorigin=&quot;anonymous&quot;&gt;
&lt;link href=&quot;styles2.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;link href=&quot;transitions.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;link href=&quot;fade.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;meta http-equiv=&quot;refresh&quot; content=&quot;35; url = gallery.html&quot; /&gt;
&lt;div class=&quot;h-100 d-flex align-items-center justify-content-center&quot;&gt;
&lt;div&gt;
&lt;h3 id=&quot;one&quot;&gt;So... How long has it been?&lt;/h3&gt;
&lt;br&gt;
&lt;h3 id=&quot;two&quot;&gt;&lt;b&gt;Let&#39;s see!&lt;/b&gt;&lt;/h2&gt;
&lt;br&gt;&lt;br&gt;
&lt;h3&gt;
&lt;div id=&quot;three&quot; class=&quot;countup&quot; id=&quot;countup1&quot;&gt;
&lt;span class=&quot;sep&quot;&gt;
&lt;span class=&quot;timeel days&quot;&gt;00&lt;/span&gt;
&lt;br&gt;
&lt;span class=&quot;timeel timeRefDays&quot;&gt;days&lt;/span&gt;
&lt;/span&gt;
&lt;span class=&quot;sep&quot;&gt;
&lt;span class=&quot;timeel hours&quot;&gt;00&lt;/span&gt;
&lt;br&gt;
&lt;span class=&quot;timeel timeRefHours&quot;&gt;hours&lt;/span&gt;
&lt;/span&gt;
&lt;span class=&quot;sep&quot;&gt;
&lt;span class=&quot;timeel minutes&quot;&gt;00&lt;/span&gt;
&lt;br&gt;
&lt;span class=&quot;timeel timeRefMinutes&quot;&gt;minutes&lt;/span&gt;
&lt;/span&gt;
&lt;span class=&quot;sep&quot;&gt;
&lt;span class=&quot;timeel seconds&quot;&gt;00&lt;/span&gt;
&lt;br&gt;
&lt;span class=&quot;timeel timeRefSeconds&quot;&gt;seconds&lt;/span&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;/h3&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

Here is the CSS.

.countup .sep {
display: inline-block;
padding: 50px;
background: rgba(0, 128, 128, 0.707);
margin: 0;
color: rgb(0, 0, 0);
min-width: 2.6rem;
border-radius: 10px;
}

I'd be grateful for any help on this!

I tried moving around the script tag to the body, to above bootstrap and to under bootstrap. I also tried adding a window.onload to the JS but that didn't work either. Help?

答案1

得分: 0

错误在这一行:

&lt;div id=&quot;three&quot; class=&quot;countup&quot; id=&quot;countup1&quot;&gt;

你定义了两个不同的 id,并且 JavaScript 代码找不到 id 为 "countup1" 的元素。
只需删除该行中的 id "three" 即可。

英文:

The error is in the line:

&lt;div id=&quot;three&quot; class=&quot;countup&quot; id=&quot;countup1&quot;&gt;

You defined two differents id, and the javascript code can't find the id "countup1".
Just delete the id "three" in the line.

答案2

得分: 0

在原始代码中,将ID参数传递给了countUpFromTime函数,并尝试使用指定的ID和它们的类名来检索元素。然而,似乎无法使用类名找到这些元素。为了解决这个问题,我修改了代码,将基于类的元素检索替换为直接使用元素的ID进行选择。

在修改后的代码中,我删除了countUpFromTime函数中的ID参数,因为它不再需要。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

window.addEventListener(&#39;load&#39;, (event) =&gt; {
countUpFromTime(&quot;July 9, 2021 12:00:00&quot;);
});
function countUpFromTime(countFrom) {
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);
document.getElementById(&#39;days&#39;).innerHTML = days;
document.getElementById(&#39;hours&#39;).innerHTML = hours;
document.getElementById(&#39;minutes&#39;).innerHTML = mins;
document.getElementById(&#39;seconds&#39;).innerHTML = secs;
clearTimeout(countUpFromTime.interval);
countUpFromTime.interval = setTimeout(function () { countUpFromTime(countFrom); }, 1000);
};

<!-- language: lang-css -->

.countup .sep {
display: inline-block;
padding: 50px;
background: rgba(0, 128, 128, 0.707);
margin: 0;
color: rgb(0, 0, 0);
min-width: 2.6rem;
border-radius: 10px;
}

<!-- language: lang-html -->

&lt;div&gt;
&lt;h3 id=&quot;one&quot;&gt;So... How long has it been?&lt;/h3&gt;
&lt;br&gt;
&lt;h3 id=&quot;two&quot;&gt;&lt;b&gt;Let&#39;s see!&lt;/b&gt;&lt;/h2&gt;
&lt;br&gt;&lt;br&gt;
&lt;h3&gt;
&lt;div id=&quot;three&quot; class=&quot;countup&quot; id=&quot;countup1&quot;&gt;
&lt;span class=&quot;sep&quot;&gt;
&lt;span class=&quot;timeel&quot; id=&quot;days&quot;&gt;00&lt;/span&gt;
&lt;br&gt;
&lt;span class=&quot;timeel timeRefDays&quot;&gt;days&lt;/span&gt;
&lt;/span&gt;
&lt;span class=&quot;sep&quot;&gt;
&lt;span class=&quot;timeel&quot; id=&quot;hours&quot;&gt;00&lt;/span&gt;
&lt;br&gt;
&lt;span class=&quot;timeel timeRefHours&quot;&gt;hours&lt;/span&gt;
&lt;/span&gt;
&lt;span class=&quot;sep&quot;&gt;
&lt;span class=&quot;timeel&quot; id=&quot;minutes&quot;&gt;00&lt;/span&gt;
&lt;br&gt;
&lt;span class=&quot;timeel timeRefMinutes&quot;&gt;minutes&lt;/span&gt;
&lt;/span&gt;
&lt;span class=&quot;sep&quot;&gt;
&lt;span class=&quot;timeel&quot; id=&quot;seconds&quot;&gt;00&lt;/span&gt;
&lt;br&gt;
&lt;span class=&quot;timeel timeRefSeconds&quot;&gt;seconds&lt;/span&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;/h3&gt;
&lt;/div&gt;

<!-- end snippet -->

In the original code, an ID parameter was passed to the countUpFromTime function, and an attempt was made to retrieve elements using the specified ID and their class names. However, it appears that these elements could not be found using class names. To address this, I modified the code by replacing the class-based element retrieval with direct selection using the elements' IDs.

In the modified code, I removed the ID parameter from the countUpFromTime function because it was no longer necessary.

huangapple
  • 本文由 发表于 2023年7月10日 10:28:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定