I'm doing a project that is a calculator.
When loading the page, I would like to show the current date and time.
I added setInterval
to refresh the time every second (I'm showing hh-mm-ss) and update the date automatically when needed.
My code so far looks like this:
class CalcController {
constructor() {
this._locale = 'pt-BR';
const $ = document.querySelector.bind(document);
//As próximas três linhas são respectivamente o elemento dos
//números da calculadora, a data e a hora no HTML
this._displayCalcEl = $('#display');
this._dateEl = $('#data');
this._timeEl = $('#hora');
this._displayCalc = '0';
this._currentDate = new Date();
this.init();
}
init() {
this._displayCalcEl.innerHTML = this._displayCalc;
this.setDisplayDateTime();
//A parte que não está funcionando
setInterval(() => this.setDisplayDateTime(), 1000);
}
setDisplayDateTime() {
this._dateEl.innerHTML = this._currentDate.toLocaleDateString(this._locale, {
day: '2-digit',
month: 'long',
year: 'numeric',
});
this._timeEl.innerHTML = this._currentDate.toLocaleTimeString(this._locale);
}
}
When the page loads, it even shows everything I want, the problem is that when it comes to updating with setInterval, it does not work.