You set the variable
z
once, when the
setAlarm
function is called. Each time your
displayHello
function is called, you are testing the original value of
z
against the current value of
alarm.value
. Unless the user initially enters the current time, or updates the
alarm
input to the current time, that test will never pass.
Cache the value of
alarm.value
, and move the calculation of
z
inside the
displayHello
function.
function setAlarm(e) {
e.preventDefault();
const alarmTime = document.getElementById('alarm').value;
const myInterval = setInterval(displayHello, 1000);
function displayHello(){
const d = new Date();
const a = d.getHours();
const b = d.getMinutes();
const c = d.getSeconds();
const z = `${a}:${b}:${c}`;
if (z === alarmTime) {
console.log("time is valid")
const failuree = document.getElementById('alarm');
failuree.classList.add('alert-success');
clearInterval(myInterval);
} else {
console.log("Invalid time")
const failure = document.getElementById('failure');
failure.classList.add('show');
}
}
}