我正在创建一个显示当前运行时间的秒表。我需要创建一个单个按钮,该按钮通过" start"停止"one_answers"重置"循环。
它应该从" start"开始,当单击时,它将变为"停止",然后以"重置"结尾。并循环通过它们。
我找不到在线解释这一点的指南。而且我似乎无法自己弄清楚。
start = document.getElementById('Start');
stop = document.getElementById('Stop');
reset = document.getElementById('Reset');
let watchRunning = false;
Start.addEventListener('click', startHandler);
Stop.addEventListener('click', stopHandler);
Reset.addEventListener('click', resetHandler);
function startHandler() {
if (!watchRunning) {
watchRunning = setInterval(update, 70);
}
}
function stopHandler() {
clearInterval(watchRunning);
watchRunning = null;
}
function resetHandler() {
document.getElementById("Time").innerHTML = "00.00";
clearInterval(watchRunning);
watchRunning = false;
}
update();
var seconds;
var milliseconds;
var d;
function update() {
d = new Date();
seconds = d.getSeconds();
milliseconds = Math.floor((d.getMilliseconds() / 10));
if (milliseconds < 10 && seconds < 10) {
document.getElementById("Time").innerHTML =
"0" + seconds + ".0" + milliseconds;
} else if (milliseconds < 10 && seconds >= 10) {
document.getElementById("Time").innerHTML =
seconds + ".0" + milliseconds;
} else if (milliseconds >= 0 && seconds < 10) {
document.getElementById("Time").innerHTML =
"0" + seconds + "." + milliseconds;
} else if (milliseconds >= 0 && seconds >= 10) {
document.getElementById("Time").innerHTML =
seconds + "." + milliseconds;
}
}
#Time {
background-color: yellow;
max-width: 2.3%;
}
<h1>Stop Watch</h1>
<button id="Start">Start</button>
<button id="Stop">Stop</button>
<button id="Reset">Reset</button>
<h3>Elapsed Time:</h3>
<p id="Time"></p>
也许您可以更改思想,而不是三个按钮,将您的想法封装成一个,并将其称为秒表。每次单击按钮时,它都会进入下一个状态并执行关联的处理程序功能。这就是我的意思:
var startHandler = function (state) { console.log(state); }
var stopHandler = function (state) { console.log(state); }
var resetHandler = function (state) { console.log(state); }
var StopWatch = function (selector) {
var states = ['Start', 'Stop', 'Reset']
var handlers = [startHandler, stopHandler, resetHandler]
var currentState = 0;
var element = document.querySelector(selector);
var clickHandler = function () {
// Execute 'currentState', pass anything you want to handlers
handlers[currentState](states[currentState])
// Update currentState after, means 'move to next state'
if (currentState < (states.length - 1)) {
currentState++;
} else {
currentState = 0;
}
element.innerHTML = states[currentState];
}
return {
init: function () {
// Initial work
element.addEventListener('click', clickHandler);
},
remove: function () {
// Cleanup
element.removeEventListener('click', clickHandler)
}
}
}
StopWatch('#stopwatch').init();
这是一个可运行的示例骨架:
https://jsbin.com/zawomori/edit?html,js,console
我已经修改了您的代码以创建所需的效果。我已经使用jQuery隐藏和显示点击事件上的按钮。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css">
body{
margin: 0px;
width: 100%;
padding: 0px;
}
#Time {
background-color: yellow;
max-width: 2.3%;
}
.box{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-65%);
}
</style>
</head>
<body>
<div class="box">
<h1>Stop Watch</h1>
<button id="Start">Start</button>
<button id="Stop" >Stop</button>
<button id="Reset">Reset</button>
<h3>Elapsed Time:</h3>
<p id="Time"></p>
</div>
<script type="text/javascript">
$('document').ready(function(){
$("#Stop").prop("disabled",true);
$("#Reset").prop("disabled",true);
$("#Stop").prop("hidden",true);
$("#Reset").prop("hidden",true);
});
$('#Start').click(function(){
$("#Stop").prop("disabled",false);
$("#Reset").prop("disabled",true);
$("#Stop").prop("hidden",false);
$("#Reset").prop("hidden",true);
$("#Start").prop("disabled",true);
$("#Start").prop("hidden",true);
});
$('#Stop').click(function(){
$("#Reset").prop("disabled",false);
$("#Reset").prop("hidden",false);
$("#Stop").prop("disabled",true);
$("#Stop").prop("hidden",true);
});
$('#Reset').click(function(){
$("#Start").prop("disabled",false);
$("#Start").prop("hidden",false);
$("#Reset").prop("disabled",true);
$("#Reset").prop("hidden",true);
});
start = document.getElementById('Start');
stop = document.getElementById('Stop');
reset = document.getElementById('Reset');
let watchRunning = false;
Start.addEventListener('click', startHandler);
Stop.addEventListener('click', stopHandler);
Reset.addEventListener('click', resetHandler);
function startHandler() {
if (!watchRunning) {
watchRunning = setInterval(update, 70);
}
}
function stopHandler() {
clearInterval(watchRunning);
watchRunning = null;
}
function resetHandler() {
document.getElementById("Time").innerHTML = "00.00";
clearInterval(watchRunning);
watchRunning = false;
}
update();
var seconds;
var milliseconds;
var d;
function update() {
d = new Date();
seconds = d.getSeconds();
milliseconds = Math.floor((d.getMilliseconds() / 10));
if (milliseconds < 10 && seconds < 10) {
document.getElementById("Time").innerHTML =
"0" + seconds + ".0" + milliseconds;
} else if (milliseconds < 10 && seconds >= 10) {
document.getElementById("Time").innerHTML =
seconds + ".0" + milliseconds;
} else if (milliseconds >= 0 && seconds < 10) {
document.getElementById("Time").innerHTML =
"0" + seconds + "." + milliseconds;
} else if (milliseconds >= 0 && seconds >= 10) {
document.getElementById("Time").innerHTML =
seconds + "." + milliseconds;
}
}
</script>
</body>
</html>