Key Issues:
-
Double Decrementing (--timer): This is likely why your timer "stops early" or moves too fast. You are calling --timer twice inside the same setInterval loop: once when checking for the "warning" class and again when checking for the "expired" state. This means for every 1 second of real time, the code removes 2 seconds from the counter.
-
Overlapping Intervals: If startCountdown is called more than once (e.g., a user clicks a button twice), multiple intervals will run simultaneously. This causes the timer to flicker or count down at warp speed because each interval is fighting to update the same display element.
-
Scope and Garbage Collection: The interval variable is defined locally within the function. While the listener for the "compareButton" can access it via a closure, if the function is re-run, the reference to the previous interval is lost, making it impossible to clear the old timer.
-
Event Listener Stacking: Every time the disableBackspaceCheckbox is toggled, a new event listener