-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
124 lines (105 loc) · 3.33 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Global variables
let startTime;
let timerInterval;
let isTiming = false;
let difficulty = 1; // 0 for easy, 1 for medium, 2 for hard, 3 for extreme
const difficulties = [20, 15, 10, 6]; // Corresponding times for each difficulty level in seconds
// Event listeners
document.body.addEventListener('keydown', function(event) {
if (event.code === 'Space') {
event.preventDefault();
toggleTimer();
}
});
document.body.addEventListener('click', function(event) {
toggleTimer();
});
document.getElementById('easyBtn').addEventListener('click', function() {
toggleButton(this);
changeDifficulty(0);
});
document.getElementById('mediumBtn').addEventListener('click', function() {
toggleButton(this);
changeDifficulty(1);
});
document.getElementById('hardBtn').addEventListener('click', function() {
toggleButton(this);
changeDifficulty(2);
});
document.getElementById('extremeBtn').addEventListener('click', function() {
toggleButton(this);
changeDifficulty(3);
});
document.getElementById('stopBtn').addEventListener('click', function() {
isTiming = false;
clearInterval(timerInterval);
// Display the time taken
displayTime();
});
// Functions
function toggleTimer() {
if (!isTiming) {
isTiming = true;
startTime = Date.now();
timerInterval = setInterval(updateTimer, 10);
} else {
isTiming = false;
clearInterval(timerInterval);
}
}
function changeDifficulty(difficultyLevel) {
isTiming = false; // Stop the timer before changing the difficulty
difficulty = difficultyLevel;
// Update the difficulty level text
document.getElementById('difficulty-level').textContent = `Difficulty: ${getDifficultyText(difficultyLevel)}`;
// Start the timer again with the new difficulty
isTiming = true;
startTime = Date.now();
timerInterval = setInterval(updateTimer, 10);
}
function toggleButton(button) {
// Remove the active class from all buttons
const buttons = document.querySelectorAll('.button');
for (const button of buttons) {
button.classList.remove('active');
}
// Add the active class to the clicked button
button.classList.add('active');
// Update the difficulty level
changeDifficulty(button.id);
}
function updateTimer() {
if (isTiming) {
const currentTime = Date.now();
const elapsedTime = (currentTime - startTime) / 1000;
document.getElementById('timer').textContent = `Time: ${elapsedTime.toFixed(1)} seconds`;
if (elapsedTime >= difficulties[difficulty] && elapsedTime <= difficulties[difficulty] + 2) {
playTingSound();
}
}
}
function displayTime() {
// Display the time taken
const elapsedTime = (Date.now() - startTime) / 1000;
const messageElement = document.getElementById('message');
messageElement.textContent = `Time taken: ${elapsedTime.toFixed(1)} seconds`;
}
function playTingSound() {
const tingSound = document.getElementById('tingSound');
tingSound.play();
}
function getDifficultyText(difficultyLevel) {
switch (difficultyLevel) {
case 0:
color = 'green';
return 'Easy';
case 1:
return 'Medium';
case 2:
return 'Hard';
case 3:
return 'Extreme';
default:
return 'Unknown';
}
}