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_i.html
37 lines ASCII Windows (CRLF)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS Timers I</title>
</head>
<body>
<h1>JS Timers I</h1>
<button onclick="startNewTimer()">Start</button>
<div id="timers"></div>
<script>
function startNewTimer() {
let timerContainer = document.getElementById("timers");
let newTimerId = 'timer' + Date.now();
let newTimerElement = document.createElement("h2");
newTimerElement.id = newTimerId;
newTimerElement.innerHTML = "Stoppur: 0.00 sekunder.";
timerContainer.appendChild(newTimerElement);
let count = 0;
let intervalId = setInterval(function() {
count++;
document.getElementById(newTimerId).innerHTML = "Stoppur: " + (count / 100).toFixed(2) + " sekunder.";
}, 10);
let stopButton = document.createElement("button");
stopButton.innerHTML = "Stop";
stopButton.onclick = function() {
clearInterval(intervalId);
stopButton.disabled = true;
};
timerContainer.appendChild(stopButton);
}
</script>
</body>
</html>