是否有一种方法从JavaScript的setTimeout循环退出if语句?



我正在创建一个百分比时钟,它显示我在一天中的特定时间内完成了多少百分比。当我运行它时,它的输出超过了100%,而当时它应该是50%。我意识到错误是它没有退出if语句,我找不到这样做的方法。我不能使用while true语句,因为它们不能与浏览器一起工作,所以如果有人能找到一种方法来退出if语句时,秒变量得到一定的量,我会非常感激。下面是我的代码:

var clock = document.getElementById("clockHeading");

setInterval(() => {

var today = new Date();
var hours = today.getHours() * 3600;
var minutes = today.getMinutes() * 60;
var seconds = today.getSeconds() + minutes + hours;
seconds = seconds - 31800;
if (seconds >= 0 && seconds <= 4140) {
setInterval(() => {
var percentage = seconds / 4140;
percentage = percentage * 100;
percentage = percentage.toFixed(1);
percentage = String(percentage) + '%';
clock.innerHTML = percentage;
}, 1000)
}
if (seconds >= 4200 && seconds <= 7380) {
setInterval(() => {
var percentage = seconds / 3240;
percentage = percentage * 100;
percentage = percentage.toFixed(1);
percentage = String(percentage) + '%';
clock.innerHTML = percentage;
}, 1000)
}
if (seconds >= 8640 && seconds <= 11700) {
setInterval (() => {
var percentage = seconds / 3060;
percentage = percentage * 100;
percentage = percentage.toFixed(1);
percentage = String(percentage) + '%';
clock.innerHTML = percentage;
}, 1000)
}
if (seconds >= 11760 && seconds <= 14700) {
setInterval (() => {
var percentage = seconds / 2940;
percentage = percentage * 100;
percentage = percentage.toFixed(1);
percentage = String(percentage) + '%';
clock.innerHTML = percentage;
}, 1000)
}
if (seconds >= 17520 && seconds <= 20700) {
setInterval (() => {
var percentage = seconds / 3180;
percentage = percentage * 100;
percentage = percentage.toFixed(1);
percentage = String(percentage) + '%';
clock.innerHTML = percentage;
}, 1000)
}
if (seconds >= 20760 && seconds <= 24000) {
setInterval (() => {
var percentage = seconds / 3240;
percentage = percentage * 100;
percentage = percentage.toFixed(1);
percentage = String(percentage) + '%';
clock.innerHTML = percentage;
}, 1000)
}
}, 1000)
body{
font-family: 'Josefin Sans', sans-serif;
}
#clockHeading{
position: absolute;
top: 38%;
bottom: 0%;
width: 100%;
text-align: center;
font-size: 200px;
}
* {
margin: 0;
padding: 0;
}
.animation-area {
background: linear-gradient(to left, #8942a8, #ba382f);
width: 100%;
height: 100vh;
}
.box-area {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
.box-area li {
position: absolute;
display: block;
list-style: none;
width: 25px;
height: 25px;
background: rgba(255, 255, 255, 0.2);
animation: animate 20s linear infinite;
bottom: -150px;
}
.box-area li:nth-child(1) {
left: 86%;
width: 80px;
height: 80px;
animation-delay: 0s;
}
.box-area li:nth-child(2) {
left: 12%;
width: 30px;
height: 30px;
animation-delay: 1.5s;
animation-duration: 10s;
}
.box-area li:nth-child(3) {
left: 70%;
width: 100px;
height: 100px;
animation-delay: 5.5s;
}
.box-area li:nth-child(4) {
left: 42%;
width: 150px;
height: 150px;
animation-delay: 0s;
animation-duration: 15s;
}
.box-area li:nth-child(5) {
left: 65%;
width: 40px;
height: 40px;
animation-delay: 0s;
}
.box-area li:nth-child(6) {
left: 15%;
width: 110px;
height: 110px;
animation-delay: 3.5s;
}
@keyframes animate {
0% {
transform: translateY(0) rotate(0deg);
opacity: 1;
}
100% {
transform: translateY(-800px) rotate(360deg);
opacity: 0;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Percent Clock</title>
<link href="style.css" rel="stylesheet">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Josefin+Sans:ital,wght@1,300&display=swap" rel="stylesheet">
</head>
<body>
<h1 id="clockHeading"></h1>
<script type="text/javascript" src="clock.js"></script>
<div class="animation-area">
<ul class="box-area">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</body>
</html>

每次想要"退出"时都需要clearInterval()。循环。您还需要将interval变量存储在能够清除它的地方:

请参阅我的// comments查看我所做的更改:

var interval = setInterval(() => { // Store it in a variable

var today = new Date();
var hours = today.getHours() * 3600;
var minutes = today.getMinutes() * 60;
var seconds = today.getSeconds() + minutes + hours;
seconds = seconds - 31800;
if (seconds >= 0 && seconds <= 4140) {
clearInterval( interval ); // Clear the old one
interval = setInterval(() => { // Store the new one
var percentage = seconds / 4140;
percentage = percentage * 100;
percentage = percentage.toFixed(1);
percentage = String(percentage) + '%';
clock.innerHTML = percentage;
}, 1000)
}
if (seconds >= 4200 && seconds <= 7380) {
clearInterval( interval ); // Clear the old one
interval = setInterval(() => {
var percentage = seconds / 3240;
percentage = percentage * 100;
percentage = percentage.toFixed(1);
percentage = String(percentage) + '%';
clock.innerHTML = percentage;
}, 1000)
}
if (seconds >= 8640 && seconds <= 11700) {
clearInterval( interval ); // Clear the old one
interval = setInterval (() => { // Store the new one
var percentage = seconds / 3060;
percentage = percentage * 100;
percentage = percentage.toFixed(1);
percentage = String(percentage) + '%';
clock.innerHTML = percentage;
}, 1000)
}
if (seconds >= 11760 && seconds <= 14700) {
clearInterval( interval ); // Clear the old one
interval = setInterval (() => { // Store the new one
var percentage = seconds / 2940;
percentage = percentage * 100;
percentage = percentage.toFixed(1);
percentage = String(percentage) + '%';
clock.innerHTML = percentage;
}, 1000)
}
if (seconds >= 17520 && seconds <= 20700) {
clearInterval( interval ); // Clear the old one
interval = setInterval (() => { // Store the new one
var percentage = seconds / 3180;
percentage = percentage * 100;
percentage = percentage.toFixed(1);
percentage = String(percentage) + '%';
clock.innerHTML = percentage;
}, 1000)
}
if (seconds >= 20760 && seconds <= 24000) {
clearInterval( interval ); // Clear the old one
interval = setInterval (() => { // Store the new one
var percentage = seconds / 3240;
percentage = percentage * 100;
percentage = percentage.toFixed(1);
percentage = String(percentage) + '%';
clock.innerHTML = percentage;
}, 1000)
}
}, 1000)

最新更新