In the snippet, I want the color of timer to change to red before the alert pops up.
I have written $("#timer").css('color', 'red'); before alert("Time Up!"), so logically it should change the text color before showing the alert, but it is changing color after displaying the alert.
let time = 3;
$(function() {
let interval = setInterval(function() {
time--;
if (time >= 0)
document.getElementById("timer").innerHTML = time + "";
else {
$("#timer").css('color', 'red');
clearInterval(interval);
alert("Time Up!")
}
}, 1000);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1 id="timer">3</h1>