reaksjonstid not working yet

This commit is contained in:
Geir Okkenhaug Jerstad 2024-09-18 12:20:01 +02:00
parent 9cda2e09ce
commit 166b85b5e7

View file

@ -1,15 +1,23 @@
// model // model
var app = document.getElementById('app'); var app = document.getElementById('app');
var html = ' '; var html = ' ';
var lampArray = []; var lampArray = [];
var selectedLampIndex = 0;
var lightOn; 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 // view
updateView(); updateView();
twentyFive(); twentyFive();
lightUpRandomLamp(); lightUpRandomLamp();
function updateView() { function updateView() {
app.innerHTML = html + `<button onclick="lightUpRandomLamp()">Light up random lamp</button>`; app.innerHTML = html + /*HTML*/` <h2>Klikk på en lampe for å skifte</h2><div id="showTime">timeSpent</div>`;
} }
// controller // controller
@ -30,11 +38,17 @@ function twentyFive() {
function lightUpRandomLamp() { function lightUpRandomLamp() {
checkForLightOn(); checkForLightOn();
let divToReplace = ' '; let divToReplace = ' ';
let selectedLampIndex = Math.floor(Math.random() * lampArray.length); selectedLampIndex = Math.floor(Math.random() * lampArray.length);
divToReplace = document.getElementById(selectedLampIndex); divToReplace = document.getElementById(selectedLampIndex);
let divToreplaceWith = document.getElementById(selectedLampIndex).classList = `circles lightOn` 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(); clearScreen();
} }
function clearScreen() { function clearScreen() {
html = ''; html = '';
@ -42,8 +56,26 @@ function clearScreen() {
function checkForLightOn() { function checkForLightOn() {
for (i = 0; i < lampArray.length; i++) { for (i = 0; i < lampArray.length; i++) {
document.getElementById(i).classList.contains('circles') document.getElementById(i).classList.contains('circles')
if (document.getElementById(i).classList.contains("lightOn")) { if (document.getElementById(i).classList.contains("lightOn")) {
document.getElementById(i).classList = `circles`; 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)
}