Можно ли запустить функцию, находясь в setInterval? - PullRequest
0 голосов
/ 23 апреля 2020

У меня есть setInterval(), это обратный отсчет на экране. У меня есть метод, который должен срабатывать, когда часы достигают -5 минут. Эта часть покрыта, метод вызывается, однако, он не работает. Причина, по которой я говорю, что он вызывается, но он не запускается, заключается в том, что у меня есть console.log в первой строке метода, чтобы сообщить мне, что он был вызван, но ничего не происходит. Там он умирает.

На данный момент метод создаст для меня объект и console.log.

здесь:

установить интервал

return this.timeRef = setInterval(() => {
    this.counter = startTime - Date.now();
    this.hours = Math.floor((this.counter / (1000 * 60 * 60)) % 24);
    this.minutes = Math.floor( (this.counter / 60000) % 60 );
    this.seconds = Math.floor(Math.floor(this.counter % 60000) / 1000);

    // Hours
    if (Math.sign(this.hours) !== -1) {
        if (this.hours < 10) {
            this.hours = '0' + this.hours;
        }
    } else {
        this.hours = '-0' + ( -this.hours - 1);
    }

    // Minutes
    if (Math.sign(this.minutes) !== -1) {
        if (this.minutes < 10) {
            this.minutes = '0' + this.minutes;
        }
    } else {
        // If it's 5 minutes after the end of the shift.
        // tslint:disable-next-line: radix
        if (parseInt(this.hours) <= 0 && -this.minutes === 16) {
            if (smsSent === false) {
                this.sendRdSMS();  // HERE IS WHERE THE METHOD GETS CALLED.
            }
            smsSent = true;
        }
        if (-this.minutes < 10) {
            this.minutes = '0' + -this.minutes;
        } else {
            this.minutes = -this.minutes;
        }
    }

    // Seconds
    if (Math.sign(this.seconds) !== -1) {
        if (this.seconds < 10) {
            this.seconds = '0' + this.seconds;
        }
    } else {
        if (-this.seconds === 60) {
            this.seconds = '00';
        } else if (-this.seconds < 10) {
            this.seconds = '0' + -this.seconds;
        } else {
            this.seconds = -this.seconds;
        }
    }

    // Send Local Push Notification
    if (this.hours === '00' && this.minutes === '05') {
        if (alertSent === false) {
            const localMessage = `Hey ${this.user$.firstName}! \
                            This is a reminder that your time is almost up. \
                            We hope you had an amazing a productive day.;
            this.alertTrigger(localMessage);
        }
        alertSent = true;
    }

}, 1000);

Вот метод, который должен работать

sendRdSMS() {
    console.log('called sendSMS');
    this.userUservice.getRD(this.user$.rd)
        .then(
            (rdData: User) => {
                const smsObj = {
                    rdPhone: rdData.cellphone,
                    rdName: `${rdData.firstName}`,
                    userName: `${this.user$.firstName} ${this.user$.lastName}`,
                    isEmergency: false
                };
                console.log(smsObj);
                const callable = this.cloudFunctions.httpsCallable('sendSMS');
                callable(smsObj)
                    .then(
                        response => {
                            console.log('ok');
                        }
                    )
                    .catch(
                        (err) => console.log(err)
                    )
                    .finally(
                        () => console.log('it ran')
                    );
            }
        )
        .catch(
            (err) => console.log(err)
        )
        .finally(
            () => console.log('it ran')
        );
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...