帮我改进一下这段代码<!DOCTYPE html> <html> <head> <title>计时器和计数器联动</title> <style> body { background-image: url("https://photo.tuchong.com/16490599/f/1198392745.jpg"); background-size: cover; background-repeat: no-repeat; font-family: Arial, sans-serif; } .timer-container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 400px; } .timer { font-size: 100px; color: black; margin-bottom: 50px; } .counter { font-size: 120px; color: red; } </style> </head> <body> <div class="timer-container"> <div class="timer">00:00:00</div> <div class="counter">0.00000</div> </div> <script> var timer = document.querySelector('.timer'); var counter = document.querySelector('.counter'); var startTime = Date.now(); var lastTime = startTime; var count = 0; function updateTime() { var currentTime = Date.now(); var timeDiff = currentTime - startTime; var hours = Math.floor(timeDiff / (1000 * 60 * 60)); var minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((timeDiff % (1000 * 60)) / 1000); var milliseconds = Math.floor((timeDiff % 1000) / 10); var timeString = hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0'); timer.innerHTML = timeString; count += 0.00182; counter.innerHTML = count.toFixed(5); lastTime = currentTime; } setInterval(updateTime, 1); </script> </body> </html>
时间: 2024-03-01 21:49:18 浏览: 83
这段代码已经很不错了,但是可以稍微改进一下:
1. 在样式部分,可以为元素添加 box-sizing:border-box;,以确保 padding 和 border 不会影响元素的实际宽度和高度。
2. 在 JavaScript 部分,可以将计时器和计数器的更新分别放入不同的函数中,以提高代码的可读性和可维护性。
下面是改进后的代码:
<!DOCTYPE html>
<html>
<head>
<title>计时器和计数器联动</title>
<style>
body {
background-image: url("https://photo.tuchong.com/16490599/f/1198392745.jpg");
background-size: cover;
background-repeat: no-repeat;
font-family: Arial, sans-serif;
box-sizing: border-box; /* 添加这一行 */
}
.timer-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 400px;
}
.timer {
font-size: 100px;
color: black;
margin-bottom: 50px;
}
.counter {
font-size: 120px;
color: red;
}
</style>
</head>
<body>
<div class="timer-container">
<div class="timer">00:00:00</div>
<div class="counter">0.00000</div>
</div>
<script>
var timer = document.querySelector('.timer');
var counter = document.querySelector('.counter');
var startTime = Date.now();
var lastTime = startTime;
var count = 0;
function updateTime() {
var currentTime = Date.now();
var timeDiff = currentTime - startTime;
var hours = Math.floor(timeDiff / (1000 * 60 * 60));
var minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((timeDiff % (1000 * 60)) / 1000);
var milliseconds = Math.floor((timeDiff % 1000) / 10);
var timeString = hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0');
timer.innerHTML = timeString;
lastTime = currentTime;
}
function updateCount() {
count += 0.00182;
counter.innerHTML = count.toFixed(5);
}
setInterval(updateTime, 1);
setInterval(updateCount, 5); /* 每 5 毫秒更新一次计数器 */
</script>
</body>
</html>
阅读全文