Fix bugs in script.js.

This commit is contained in:
Jason McPheron 2024-04-22 08:02:01 -07:00
parent 20483d4c40
commit 6d05755825

View file

@ -1,6 +1,10 @@
let timerRunning = false; let timerRunning = false;
let startTime; let startTime;
let elapsedTime = 0; let elapsedTime = 0;
let timerHistory = [];
let challengeRunning = false;
let challengeStartTime;
let challengeTimes = [];
let ghostGameRunning = false; let ghostGameRunning = false;
let ghostTimer; let ghostTimer;
let ghostAppearTime; let ghostAppearTime;
@ -28,29 +32,46 @@ document.getElementById('bustGhost').addEventListener('click', function() {
document.getElementById('bustGhost').style.display = 'none'; document.getElementById('bustGhost').style.display = 'none';
}); });
function jjm(){ const timerButton = document.getElementById('timerButton');
timerHistory.push(time); const historyList = document.getElementById('historyList');
const newTime = document.createElement('li'); const challengeButton = document.getElementById('challengeButton');
newTime.textContent = `Time: ${time} ms`; timerButton.addEventListener('click', function() {
historyList.appendChild(newTime); if (!timerRunning) {
} startTime = Date.now() - elapsedTime;
timerRunning = true;
timerButton.textContent = 'Stop Timer';
} else {
elapsedTime = Date.now() - startTime;
timerRunning = false;
timerButton.textContent = 'Start Timer';
recordTime(elapsedTime);
}
});
challengeButton.addEventListener('click', function() {
if (!challengeRunning) {
challengeStartTime = Date.now();
challengeRunning = true;
challengeButton.textContent = 'Tap at 2 seconds';
} else {
let tapTime = Date.now();
let interval = tapTime - challengeStartTime;
challengeTimes.push(interval);
challengeRunning = false;
challengeButton.textContent = '⏱️ 2-Second Challenge ⏱️';
recordChallengeTime(interval);
}
});
function recordTime(time) { function recordTime(time) {
const historyList = document.getElementById('historyList'); timerHistory.push(time);
const newTime = document.createElement('li'); const newTime = document.createElement('li');
newTime.textContent = `Time: ${time} ms`; newTime.textContent = `Time: ${time} ms`;
historyList.appendChild(newTime); historyList.appendChild(newTime);
} }
document.getElementById('timerButton').addEventListener('click', function() { function recordChallengeTime(time) {
if (!timerRunning) { const newTime = document.createElement('li');
startTime = Date.now(); newTime.textContent = `Attempt: ${time} ms - ${Math.abs(2000 - time)} ms off`;
timerRunning = true; resultsList.appendChild(newTime);
this.textContent = 'Stop Timer'; }
} else {
elapsedTime = Date.now() - startTime;
timerRunning = false;
this.textContent = 'Start Timer';
recordTime(elapsedTime);
}
});