javascript - 单击或按下空格键时启动/停止 Cube Timer
问题描述
我正在开发一个立方体计时器,您可以在其中按空格键/单击开始,然后按空格键/单击停止。我制作了一个可以停止/启动计时器的工作按钮,但按钮非常不切实际。
这是我到目前为止的代码:
//Define vars to hold time values
let seconds = 0;
let minutes = 0;
let hours = 0;
//Define vars to hold "display" value
let displaySeconds = 0;
let displayMinutes = 0;
let displayHours = 0;
//Define var to hold setInterval() function
let interval = null;
//Define var to hold stopwatch status
let status = "stopped";
//Stopwatch function (logic to determine when to increment next value, etc.)
function stopWatch() {
seconds++;
//Logic to determine when to increment next value
if (seconds / 60 === 1) {
seconds = 0;
minutes++;
if (minutes / 60 === 1) {
minutes = 0;
hours++;
}
}
//If seconds/minutes/hours are only one digit, add a leading 0 to the value
if (seconds < 10) {
displaySeconds = "0" + seconds.toString();
} else {
displaySeconds = seconds;
}
if (minutes < 10) {
displayMinutes = "0" + minutes.toString();
} else {
displayMinutes = minutes;
}
if (hours < 10) {
displayHours = "0" + hours.toString();
} else {
displayHours = hours;
}
//Display updated time values to user
document.getElementById("display").innerHTML = displayHours + ":" + displayMinutes + ":" + displaySeconds;
}
function startStop() {
if (status === "stopped") {
//Start the stopwatch (by calling the setInterval() function)
interval = window.setInterval(stopWatch, 1000);
document.getElementById("startStop").innerHTML = "Stop";
status = "started";
} else {
window.clearInterval(interval);
document.getElementById("startStop").innerHTML = "Start";
status = "stopped";
}
}
#time {
text-align: center;
font-size: 80px;
font-family: Roboto Mono;
}
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400&display=swap" rel="stylesheet">
<div id="time">
<div id="display">
00.00
</div>
<div class="buttons">
<button id="startStop" onclick="startStop()">Start</button>
</div>
</div>
我尝试做一个事件监听器,但我尝试的代码不起作用,
document.getElementById("time").addEventListener("click", time());
function time() {
if(status === "stopped"){
//Start the stopwatch (by calling the setInterval() function)
interval = window.setInterval(stopWatch, 1000);
document.getElementById("startStop").innerHTML = "Stop";
status = "started";
}
else{
window.clearInterval(interval);
document.getElementById("startStop").innerHTML = "Start";
status = "stopped";
}
}
解决方案
主要部分在 JS 块的末尾:
document.getElementById("time").addEventListener('click', function() { startStop(); });
document.body.onkeyup = function(e){ if(e.keyCode == 32){ startStop(); }};
//Define vars to hold time values
let seconds = 0;
let minutes = 0;
let hours = 0;
//Define vars to hold "display" value
let displaySeconds = 0;
let displayMinutes = 0;
let displayHours = 0;
//Define var to hold setInterval() function
let interval = null;
//Define var to hold stopwatch status
let status = "stopped";
//Stopwatch function (logic to determine when to increment next value, etc.)
function stopWatch() {
seconds++;
//Logic to determine when to increment next value
if (seconds / 60 === 1) {
seconds = 0;
minutes++;
if (minutes / 60 === 1) {
minutes = 0;
hours++;
}
}
//If seconds/minutes/hours are only one digit, add a leading 0 to the value
if (seconds < 10) {
displaySeconds = "0" + seconds.toString();
} else {
displaySeconds = seconds;
}
if (minutes < 10) {
displayMinutes = "0" + minutes.toString();
} else {
displayMinutes = minutes;
}
if (hours < 10) {
displayHours = "0" + hours.toString();
} else {
displayHours = hours;
}
//Display updated time values to user
document.getElementById("display").innerHTML = displayHours + ":" + displayMinutes + ":" + displaySeconds;
}
function startStop() {
if (status === "stopped") {
//Start the stopwatch (by calling the setInterval() function)
interval = window.setInterval(stopWatch, 1000);
document.getElementById("startStop").innerHTML = "Stop";
status = "started";
} else {
window.clearInterval(interval);
document.getElementById("startStop").innerHTML = "Start";
status = "stopped";
}
}
document.getElementById("time").addEventListener('click', function() { startStop(); });
document.body.onkeyup = function(e){ if(e.keyCode == 32){ startStop(); }};
#time {
text-align: center;
font-size: 80px;
font-family: Roboto Mono;
}
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400&display=swap" rel="stylesheet">
<div id="time">
<div id="display">
00.00
</div>
<div class="buttons">
<button id="startStop" onclick="startStop()">Start</button>
</div>
</div>
推荐阅读
- r - R Markdown 找不到对象
- css - CSS 网格 - 响应式画廊
- java - 添加 aws-java-sdk 依赖项后 Maven 错误“206 文件名或扩展名太长”
- python - 在 Python 中交替使用类/全局变量和闭包
- c++ - 主游戏循环中的时间计算
- spring-boot - 全局注册 DynamicParameterizedType
- python - 没有这样的文件或目录:使用 pyinstaller 将文件从 .py 编译为 .exe 后出现“Temp\\_MEI79882\\currency_converter\\eurofxref-hist.zip”错误
- session - Phalcon 框架如何以及何时创建新的会话文件?
- cypress - 赛普拉斯:在赛普拉斯中分配和访问全局变量
- dom - 触发事件时获取 ID 属性(订阅)