gpt4 book ai didi

javascript - 第二秒表失灵

转载 作者:行者123 更新时间:2023-12-02 14:02:36 25 4
gpt4 key购买 nike

我正在尝试做一个简单的页面,其中有 8 个秒表,第二个秒表不起作用,我似乎无法弄清楚,有什么想法吗?第一个脚本正常工作,但是当我点击开始时,第二个脚本会给我一秒钟,然后我可以暂停并恢复并再获得一秒钟。这对我来说看起来很正确,但我对此还很陌生。

			var time = 0;
var running = 0;

function startPause()
{
if(running == 0){
running = 1;
increment();
document.getElementById("startPause").innerHTML = "Pause";
}else{
running = 0;
document.getElementById("startPause").innerHTML = "Resume";
}
}

function reset()
{
running = 0;
time = 0;
document.getElementById("startPause").innerHTML = "Start";
document.getElementById("output").innerHTML = "00:00:00";
}

function increment()
{
if(running == 1){
setTimeout(function(){
time++;
var mins = Math.floor(time/10/60);
var secs = Math.floor(time/10 % 60);
var tenths = time % 10;

if(mins < 10){
mins = "0" + mins;
}
if(secs < 10){
secs = "0" + secs;
}
document.getElementById("output").innerHTML = mins + ":" + secs + ":" + "0" + tenths;
increment();

},100);
}
}
var time2 = 0;
var running2 = 0;

function startPause2()
{
if(running2 == 0){
running2 = 1;
increment2();
document.getElementById("startPause2").innerHTML = "Pause";
}
else{
running2 = 0;
document.getElementById("startPause2").innerHTML = "Resume";
}
}

function reset2()
{
running2 = 0;
time2 = 0;
document.getElementById("startPause2").innerHTML = "Start";
document.getElementById("output2").innerHTML = "00:00:00";
}

function increment2()
{
if(running2 == 1){
setTimeout(function(){
time2++;
var mins2 = Math.floor(time2/10/60);
var secs2 = Math.floor(time2/10 % 60);
var tenths2 = time2 % 10;

if(mins2 < 10){
mins2 = "0" + mins2;
}
if(secs2 < 10){
secs2 = "0" + secs2;
}
document.getElementById("output2").innerHTML = mins2 + ":" + secs2 + ":" + "0" + tenths2;
increment();

},100);
}
}
html {

size: 100%,100%;
background-color: #f2f2f2;
}

h1{
font-Family: Arial;
color: #5573A9;
font-size: 40px;
position: static;
}

body {
display: block;
margin:8px, dotted line;
background-color: #f2f2f2;


}




#output{
position:center;
width:120px;
height:25px;
background-color:#CCC;
border:3px solid #999;
}

#output2{
position: relative;
width:120px;
height:25px;
background-color:#CCC;
border:3px solid #999;
}

table {
display:table;
border-collapse:seperate;
border-spacing: 52px;
border-color: #FFF;
}
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">

<title>Time Study</title>
<h1>Time Study</h1>
</head>

<body>
<table>
<tr>
<th>Bin</th>
<th>Bulk</th>
</tr>
<tr>
<td>
<p id="output"></p>
<div id="controls">
<button id="startPause" onclick="startPause()">Start</button>
<button onclick="reset()">Reset</button>
</div>
</td>
<td>
<p id="output2"></p>
<div id="controls2">
<button id="startPause2" onclick="startPause2()">Start</button>
<button onclick="reset2()">Reset</button>
</div>
</td>
</tr>
</table>
</body>
</html>

最佳答案

快速回答是:

对于第二个计时器,当您需要调用 increment2() 时,您正在调用 increment()

较长的答案是:

这指出了您尝试解决此问题的方式中的缺陷。如果您最终想要 8 个计时器,这将是一个大烂摊子。

您想要做的是尝试解决问题,以便您的大部分代码被重用而不是复制。如果您需要这方面的帮助,请告诉我。

请在下面的 JS 中查找我的评论以进行修复。

var time = 0;
var running = 0;

function startPause() {
if (running == 0) {
running = 1;
increment();
document.getElementById("startPause").innerHTML = "Pause";
} else {
running = 0;
document.getElementById("startPause").innerHTML = "Resume";
}
}

function reset() {
running = 0;
time = 0;
document.getElementById("startPause").innerHTML = "Start";
document.getElementById("output").innerHTML = "00:00:00";
}

function increment() {
if (running == 1) {
setTimeout(function() {
time++;
var mins = Math.floor(time / 10 / 60);
var secs = Math.floor(time / 10 % 60);
var tenths = time % 10;

if (mins < 10) {
mins = "0" + mins;
}

if (secs < 10) {
secs = "0" + secs;
}

document.getElementById("output").innerHTML = mins + ":" + secs + ":" + "0" + tenths;

increment();

}, 100);
}
}
var time2 = 0;
var running2 = 0;

function startPause2() {
if (running2 == 0) {
running2 = 1;
increment2();
document.getElementById("startPause2").innerHTML = "Pause";
} else {
running2 = 0;
document.getElementById("startPause2").innerHTML = "Resume";
}
}

function reset2() {
running2 = 0;
time2 = 0;
document.getElementById("startPause2").innerHTML = "Start";
document.getElementById("output2").innerHTML = "00:00:00";
}

function increment2() {
if (running2 == 1) {
setTimeout(function() {
time2++;
var mins2 = Math.floor(time2 / 10 / 60);
var secs2 = Math.floor(time2 / 10 % 60);
var tenths2 = time2 % 10;

if (mins2 < 10) {
mins2 = "0" + mins2;
}

if (secs2 < 10) {
secs2 = "0" + secs2;
}

document.getElementById("output2").innerHTML = mins2 + ":" + secs2 + ":" + "0" + tenths2;

/// ===============================
/// This is your fix
/// ===============================
// increment();
increment2();
/// ===============================

}, 100);
}
}
html {
size: 100%, 100%;
background-color: #f2f2f2;
}
h1 {
font-Family: Arial;
color: #5573A9;
font-size: 40px;
position: static;
}
body {
display: block;
margin: 8px, dotted line;
background-color: #f2f2f2;
}
#output {
position: center;
width: 120px;
height: 25px;
background-color: #CCC;
border: 3px solid #999;
}
#output2 {
position: relative;
width: 120px;
height: 25px;
background-color: #CCC;
border: 3px solid #999;
}
table {
display: table;
border-collapse: seperate;
border-spacing: 52px;
border-color: #FFF;
}
<table>
<tr>
<th>Bin</th>
<th>Bulk</th>
</tr>
<tr>
<td>
<p id="output"></p>
<div id="controls">
<button id="startPause" onclick="startPause()">Start</button>
<button onclick="reset()">Reset</button>
</div>
</td>
<td>
<p id="output2"></p>
<div id="controls2">
<button id="startPause2" onclick="startPause2()">Start</button>
<button onclick="reset2()">Reset</button>
</div>
</td>
</tr>
</table>

关于javascript - 第二秒表失灵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40284674/

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