So I'm trying to figure out how to change the background color of a page after a countdown timer has been activated. For example I have code below that displays a 5:00 timer that begins counting after selecting the "Start" Button. When you press "Stop" the timer obviously stops, and when you select reset the timer resets to 5:00 and pauses. I patched the javascript code together from two codes I found online (Just now beginning javascript) and it works however I am now trying to figure out a way to change the background color of the page based on the time of the countdown timer.
So between 300 seconds and 90 seconds the color should be "GREEN" Between 90-30 the color should change to "YELLOW" 30 - 0 the color should change to "RED"
Any feedback you guys can give me on my code and how to accomplish the task above would be greatly appreciated. Im just starting out coding so if there is a proper way of doing javascript let me know.
Here is the code for my Countdown timer (javascript):
var seconds = 300;
var t;
function secondPassed() {
var minutes = Math.round((seconds - 30) / 60);
var remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
}
function countdown() {
// starts countdown
secondPassed();
if (seconds == 0) {} else {
seconds--;
t = setTimeout("countdown()", 1000);
}
}
function cdpause() {
// pauses countdown
clearTimeout(t);
};
function cdreset() {
// resets countdown
cdpause();
secondPassed();
};
@charset "UTF-8";
/* CSS Document*/
#countdown {
font-size: 2em;
background-color: white;
}
<html>
<head>
<link rel="stylesheet" type="text/css" href="newTicket2.0.css">
<script src="Timer.js">
</script>
</head>
<body onload="cdreset()">
<span id="countdown" class="timer"></span>
<input type="button" value="Start" onclick="countdown()">
<input type="button" value="Stop" onclick="cdpause()">
<input type="button" value="Reset" onclick="cdreset(seconds = 300)">
</body>
</html>
I am thinking that in order to change the background color the code should be something like this:
function changeColor() {
if (seconds == 300) {
document.change.bgColor = "green";
} else if (seconds == 90) {
document.change.bgColor = "yellow";
} else(seconds == 30) {
`enter code here`
document.change.bgColor = "red";
}
}
However, I have not been able to figure out how to change anything without messing up the original product. The color change should be linked to the button click. So once the "start" button is pressed the color should change and continue to change according to the parameters above. Once the counter has reached zero it should stay red until the reset button has been pressed. Once the "reset" button is pressed the color goes back to green and goes to yellow, then red. So it should work in tandem with the timer I just cant seem to get it to work.