我正在尝试构建一个打字测试,当用户按下文本区域中的按键时,该测试就开始倒计时。我认为 if-else 循环有助于在 HTML 中显示并启动 1 分钟倒计时器,但事实并非如此。
请向我解释我做错了什么以及如何纠正我的代码。
HTML:
<div id="timer"></div>
<p>Text for typing test will go here.</p>
<textarea id="textarea" rows="14" cols="150" placeholder="Start typing here...">
</textarea>`
Run Code Online (Sandbox Code Playgroud)
JS:
var seconds=1000 * 60; //1000 = 1 second in JS
var min = seconds * 60;
var textarea = document.getElementById("textarea").onkeypress = function() {
myFunction()
};
//When a key is pressed in the text area, update the timer using myFunction
function myFunction() {
document.getElementById("timer").innerHTML =
if (seconds>=0) {
seconds = seconds--;
} else { …Run Code Online (Sandbox Code Playgroud)