cps / script.js
Sergidev's picture
Update script.js
95a6a62 verified
// script.js
document.addEventListener('DOMContentLoaded', () => {
const clickButton = document.getElementById('clickButton');
const cpsDisplay = document.getElementById('cpsDisplay');
const timerElement = document.getElementById('timer');
let clickCount = 0;
let intervalId;
let testDuration = 15; // Duration in seconds
function startTest() {
// Reset the test state
clickCount = 0;
cpsDisplay.textContent = 'CPS: 0.00';
timerElement.textContent = testDuration;
// Ensure previous interval is cleared
if (intervalId) {
clearInterval(intervalId);
}
// Start a new timer
intervalId = setInterval(updateTimer, 1000);
// Update button state
clickButton.classList.add('white');
clickButton.textContent = clickCount;
// Set up event listeners for the button
clickButton.removeEventListener('click', startTest);
clickButton.addEventListener('click', recordClick);
}
function updateTimer() {
testDuration--;
timerElement.textContent = testDuration;
if (testDuration <= 0) {
// End the test
clearInterval(intervalId);
clickButton.classList.remove('white');
clickButton.textContent = 'Start';
clickButton.addEventListener('click', startTest);
// Display final CPS
displayFinalCPS();
// Reset the timer duration for the next test
testDuration = 15; // Reset the test duration to 15 seconds
}
}
function recordClick() {
clickCount++;
clickButton.textContent = clickCount; // Update button with the new click count
updateCPS();
}
function updateCPS() {
const cps = clickCount / (15 - testDuration);
cpsDisplay.textContent = `CPS: ${cps.toFixed(2)}`;
}
function displayFinalCPS() {
const finalCPS = clickCount / 15;
cpsDisplay.textContent = `Final CPS: ${finalCPS.toFixed(2)}`;
}
// Initialize the button with the start event listener
clickButton.addEventListener('click', startTest);
});