I am facing an issue while trying to compare a variable named 'let hours' within my class. The comparison needs to be done in a separate function called 'utcChange' after clicking a button. I initially declared this variable at the beginning as let, assuming it would be accessible anywhere. However, the if statement inside the utcChange function is not functioning as expected. Any idea why?
let hours;
class Clock {
el: Element;
constructor(element) {
this.el = element;
setInterval(() => this.run(), 1000)
}
run() {
var time = new Date();
let hours = time.getHours()+utcValue;
var hoursChanged = hours.toString();
var minutes = time.getMinutes().toString();
var seconds = time.getSeconds().toString();
if (hoursChanged.length < 2) {
hoursChanged = '0' + hoursChanged;
}
if (minutes.length < 2) {
minutes = '0' + minutes;
}
if (seconds.length < 2) {
seconds = '0' + seconds;
}
var clockStr = hours + ' : ' + minutes + ' : ' + seconds;
this.el.textContent = clockStr;
}
}
var clock = new Clock(document.getElementById('tsClock'));
var utcButton = document.getElementById('button');
utcButton.addEventListener("click", utcChange);
var utcValue = 0
function utcChange(){
if (hours < 23) {
utcValue += 1
}
}