i'm just starting out with javascript and I'm trying to make some sort of dashboard. I would like the fuel status to go down by 1 every few seconds but i want it to stop at zero. Also, i was trying to set an alert when the fuel status reaches 20%, but obviously it doesnt work.
I have a few questions: How can i "stop" the setInterval function when fuelStatus reaches 0? I dont think i fully understand the topic with the functions so i believe my approach may not be correct. Do i need to write functions for every task that i want to do like checking the fuel? Should i have put testFuel in the fuelStatus function?
Thanks guys!
var fuelStatus = 100;
function Fuel() {
fuelStatus--; //number = -1;
console.log(fuelStatus);
document.getElementById("fuelStatus").innerHTML = fuelStatus + '%';
}
setInterval(function() {
Fuel()
}, 5000)
function testFuel() {
if (fuelStatus == 20) {
alert("critical fuel status")
}
}