第二站观看失败

时间:2016-10-27 12:32:28

标签: javascript stopwatch

我正在尝试用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);
					}
				}
				
&#13;
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;
			}
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

快速回答是:

对于第二个计时器,当您需要调用 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>