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_h.html
40 lines ASCII Windows (CRLF)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS Timers H</title>
</head>
<body>
<h1>JS Timers H</h1>
<input type="number" id="inputNum" placeholder="Skriv ett heltal">
<button onclick="toggleTimer()" id="startButton">Start</button>
<h2 id="text"></h2>
<script>
let text = document.getElementById("text");
let button = document.getElementById("startButton");
let inputNum = document.getElementById("inputNum");
let nextNum = 0;
let count = 0;
let active = false;
let intervalId;
function myTimer() {
count++;
text.innerHTML += ((count * parseInt(inputNum.value)) * 2).toString() + ", ";
}
function toggleTimer() {
if (active) {
clearInterval(intervalId);
button.innerHTML = "Start";
} else {
text.innerHTML = parseInt(inputNum.value).toString() + ", ";
intervalId = setInterval(myTimer, 2000);
button.innerHTML = "Stop";
}
active = !active;
}
</script>
</body>
</html>