From 166b85b5e7e3709aaf84e857ab3283b0c78cc902 Mon Sep 17 00:00:00 2001 From: Geir Okkenhaug Jerstad Date: Wed, 18 Sep 2024 12:20:01 +0200 Subject: [PATCH] reaksjonstid not working yet --- oppgaver/uke7/reaksjonstid/mvc.js | 44 ++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/oppgaver/uke7/reaksjonstid/mvc.js b/oppgaver/uke7/reaksjonstid/mvc.js index d4833d9..dd51314 100644 --- a/oppgaver/uke7/reaksjonstid/mvc.js +++ b/oppgaver/uke7/reaksjonstid/mvc.js @@ -1,15 +1,23 @@ + + // model var app = document.getElementById('app'); var html = ' '; var lampArray = []; +var selectedLampIndex = 0; var lightOn; +var clickLamp = document.getElementById('selectedLampIndex') +var startTime = new Date().getTime(); +var finishTime = new Date().getTime(); +var spentMilliseconds = Math.floor(finishTime - startTime); +var spentSeconds = spentMilliseconds / 1000; // view updateView(); twentyFive(); lightUpRandomLamp(); function updateView() { - app.innerHTML = html + ``; + app.innerHTML = html + /*HTML*/`

Klikk på en lampe for å skifte

timeSpent
`; } // controller @@ -30,11 +38,17 @@ function twentyFive() { function lightUpRandomLamp() { checkForLightOn(); let divToReplace = ' '; - let selectedLampIndex = Math.floor(Math.random() * lampArray.length); + selectedLampIndex = Math.floor(Math.random() * lampArray.length); divToReplace = document.getElementById(selectedLampIndex); let divToreplaceWith = document.getElementById(selectedLampIndex).classList = `circles lightOn` - html = html.replace(divToReplace, divToreplaceWith); + html = html.replace(divToReplace, divToreplaceWith) + setOnClick = document.getElementById(selectedLampIndex).onclick = lightUpRandomLamp; + startTimer(); + html = html + setOnClick + clearScreen(); + + } function clearScreen() { html = ''; @@ -42,8 +56,26 @@ function clearScreen() { function checkForLightOn() { for (i = 0; i < lampArray.length; i++) { document.getElementById(i).classList.contains('circles') - if (document.getElementById(i).classList.contains("lightOn")) { - document.getElementById(i).classList = `circles`; - } + if (document.getElementById(i).classList.contains("lightOn")) { + document.getElementById(i).classList = `circles`; } } +} +function startTimer() { + clickLamp.addEventListener("click", stopTimer) + var currentTime = new Date().getTime(); + let timeSpent = Math.floor(currentTime - startTime) + let timeSpentSeconds = (timeSpent / 1000) + + console.log(timeSpentSeconds) +} + +function stopTimer() { + document.getElementById(selectedLampIndex).removeEventListener("click", stopTimer) + + var currentTime = new Date().getTime(); + let timeSpent2 = Math.floor(currentTime - startTime) + newTime = (timeSpent2 / 1000) + console.log(newTime); + document.getElementById(selectedLampIndex).addEventListener("click", startTimer) +}