我創建一個秒錶是這樣的:的addEventListener點擊不工作
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Stopwatch</title>
<link href="https://fonts.googleapis.com/css?family=Saira" rel="stylesheet">
<link href="stopwatch.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Droid+Sans" rel="stylesheet">
</head>
<body>
<div class="but">
<h1>Stopwatch</h1>
<p id="timer">0</p>
<button id="start">Start/Stop</button>
<button id="reset">Reset</button>
<button id="record">Record Time</button>
</div>
</body>
</html>
和JavaScript:
var timer = document.getElementById("timer");
var sec = 0;
document.getElementById("start").addEventListener("click", counter());
function counter(){
setInterval(time(), 10);
}
function time(){
sec++;
timer.innerHTML = sec;
}
然而,當我點擊啓動按鈕,它不工作。有人可以解釋爲什麼嗎?
非常感謝! – NewbieCoder