gpt4 book ai didi

javascript - 带有 setTimeout 的 JS 计时器得到 "Uncaught SyntaxError: Unexpected token ("

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

我一直在尝试使用 setTimeout() 命令创建一个 JavaScript 计时器。当我在浏览器控制台中查看它时,返回“未捕获的语法错误:意外的 token (”。

您还可以在此处查看代码:http://mathiakiaer.site88.net/timer/

这是 HTML 代码:

<!DOCTYPE html>
<html>
<head>
<title>Timer</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="script.js" type="text/javascript"></script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<article>
<p id="timerTime"><span id="timerTimeMinutes">00</span>:<span id="timerTimeSeconds">00</span></p>
<button id="timerStart" onclick='repeatTimer()'>START</button>
<div id="timerChangeDiv">
Minutes: <input id="timerInputMinutes" value="0" type="number" min="0" max="60"><br>
Seconds: <input id="timerInputSeconds" value="0" type="number" min="0" max="60"><br>
<button onclick="setTime()">Change Time</button>
</div>
</article>
</body>
</html>

这是 JavaScript 代码:

var seconds=0;
var minutes=0;
function countDown() {
if(seconds!==0) {
repeatTimer();
seconds--;
} else {
if(minutes!==0) {
minutes--;
seconds=59;
repeatTimer();
} else {
alert("The timer is done");
}
}
refresh();
}
function repeatTimer() {
setTimeout("function() {countDown();}", 1000);
}
function setTime() {
seconds=parseInt(document.getElementById("timerInputSeconds").value);
minutes=parseInt(document.getElementById("timerInputMinutes").value);
document.getElementById("timerInputSeconds").value=0;
document.getElementById("timerInputMinutes").value=0;
refresh();
}
function refresh() {
if(seconds>9) {
document.getElementById("timerTimeSeconds").innerHTML=seconds;
} else {
var secondsShow="0" + seconds;
document.getElementById("timerTimeSeconds").innerHTML=secondsShow;
}
if(minutes>9) {
document.getElementById("timerminutes").innerHTML=minutes;
} else {
var minutesShow="0" + minutes;
document.getElementById("timerTimeMinutes").innerHTML=minutesShow;
}
}

最佳答案

第一个参数是函数而不是字符串。

function repeatTimer() {
setTimeout(function() {countDown()}, 1000);
}

function repeatTimer() {
setTimeout(countDown, 1000);
}

关于javascript - 带有 setTimeout 的 JS 计时器得到 "Uncaught SyntaxError: Unexpected token (",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31232258/

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