Show sourcecode
The following files exists in this folder. Click to view.
public_html/gamla-kurser/webbutv2/övningar/js_timers/
js_timers_a.html
js_timers_b.html
js_timers_c.html
js_timers_d.html
js_timers_e.html
js_timers_f.html
js_timers_g.html
js_timers_h.html
js_timers_i.html
js_timers_j.html
sound/
js_timers_c.html
38 lines ASCII Windows (CRLF)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS Timers C</title>
</head>
<body>
<h1>JS Timers C</h1>
<button onclick="toggleTimer()" id="startButton">Start</button>
<h2 id="timer">Stoppur: 0.00 sekunder.</h2>
<h2 id="text"></h2>
<script>
let timer = document.getElementById("timer");
let text = document.getElementById("text");
let button = document.getElementById("startButton");
let count = 0;
let active = false;
let intervalId;
function myTimer() {
count++;
timer.innerHTML = "Stoppur: " + (count / 100).toFixed(2) + " sekunder.";
}
function toggleTimer() {
if (active) {
clearInterval(intervalId);
button.innerHTML = "Start";
} else {
intervalId = setInterval(myTimer, 10);
button.innerHTML = "Stop";
}
active = !active;
}
</script>
</body>
</html>