File size: 2,192 Bytes
cbe4551
 
 
 
 
 
 
 
 
 
 
 
858b6c2
cbe4551
 
 
 
858b6c2
 
 
 
 
 
cbe4551
 
858b6c2
 
 
 
 
cbe4551
 
 
 
 
 
 
 
 
45a75d7
cbe4551
 
45a75d7
 
 
 
cbe4551
95a6a62
 
 
cbe4551
 
 
 
 
ee1b5be
cbe4551
 
 
 
 
 
 
 
 
 
 
b784ba3
cbe4551
45a75d7
cbe4551
 
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
// 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);
});