Показать сообщение отдельно
  #2 (permalink)  
Старый 11.04.2018, 13:00
Профессор
Отправить личное сообщение для Nexus Посмотреть профиль Найти все сообщения от Nexus
 
Регистрация: 04.12.2012
Сообщений: 3,726

Странный у вас таймер, имхо.
По-моему класс Timer не должен взаимодействовать с dom, если его основной задачей является countdown.
Оставшееся время лучше хранить в (мили)секундах.

class Timer {
    __startValue;
    __currentValue;
    __state = false;

    constructor(seconds) {
        this.__startValue = this.__currentValue = seconds;
    }

    __countdown() {
        if (!this.__state)
            return this;

        this.__currentValue--;

        setTimeout(this.__countdown.bind(this), 1000);

        return this;
    }

    start() {
        this.__state = true;
        this.__countdown();

        return this;
    }

    stop() {
        this.__state = false;

        return this;
    }

    reset() {
        this.__currentValue = this.__startValue;

        return this;
    }

    getSeconds() {
        return this.__currentValue;
    }
}
Ответить с цитированием