gpt4 book ai didi

javascript - 如何在同一按钮位置循环切换多个按钮

转载 作者:行者123 更新时间:2023-12-03 00:09:39 24 4
gpt4 key购买 nike

我正在创建一个显示当前运行时间的秒表。我需要创建一个循环“开始”、“停止”和“重置”的按钮。

它应该以“开始”开始,当单击它时,它会转到“停止”,稍后它应该以“重置”结束。然后循环遍历它们。

我在网上找不到任何解释这一点的指南。我似乎无法自己解决这个问题。

        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/zawomorori/edit?html,js,console

关于javascript - 如何在同一按钮位置循环切换多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54800621/

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