Показать сообщение отдельно
  #1 (permalink)  
Старый 03.08.2022, 11:26
Профессор
Отправить личное сообщение для NovichokJS Посмотреть профиль Найти все сообщения от NovichokJS
 
Регистрация: 25.04.2022
Сообщений: 159

Таймер из объекта
Почему при выполнении кода таймер не запускается, не идет отчет времени? Где то ошибка?

const timer = {
  secondsPassed: 0,
  minsPassed: 0,
  Id: null,
  startTimer() {
    this.Id = setInterval(() => {

      this.secondsPassed += 1;

      if (this.secondsPassed === 60) {
        this.minsPassed += 1;
        this.secondsPassed = 0;
      }
    }, 1000);
  },

  stopTimer() {
    clearInterval(this.Id);
  },

  getTime() {
    return `${this.minsPassed}:${
      this.secondsPassed < 10 ? '0' + this.secondsPassed : this.secondsPassed
    }`;
  },

  resetTimer() {
    this.minsPassed = 0;
    this.secondsPassed = 0;
  },
};

console.log(timer.startTimer());
console.log(timer.getTime());
Ответить с цитированием