0

I have a setInterval() that's a countdown on the screen. I have a method that should be triggered when the clock reaches -5 minutes. That part is covered, the method is being called however, it's not running. The reason I say it's being called but it doesn't run is because I have a console.log on the first line of the method to tell me that it was called but nothing happens. It dies there.

The method, for now, will create an object and console.log for me.

here it is:

set interval

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);

Here is the method that's supposed to run

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')
        );
}
MrRobot
  • 1,001
  • 1
  • 14
  • 34
  • 1
    Maybe because you used `this.function()` when `this` does not point to your class, but to the function you passed into setInterval? – takendarkk Apr 23 '20 at 15:04
  • 1
    Yes it's possible to do anything you can do in any function inside a `setInterval()` callback. What have you done so far to debug this? Have you tried adding a `console.log()` at the point where you *call* the function? Are errors reported in the browser console? – Pointy Apr 23 '20 at 15:04
  • Did you try setting a breakpoint at the first line of the method (i.e. `console.log`? How do you know that the method is indeed invoked at all? – FDavidov Apr 23 '20 at 15:08
  • @epascarello - But his callback is an arrow function, so the inner calls should point to the expected `this`, shouldn't they? Unless, maybe, `setInterval` itself is called inside a function which sets `this` to something else. In any event, reading [this post](https://stackoverflow.com/q/20279484/1009922) from Felix Kling could help the OP. – ConnorsFan Apr 23 '20 at 15:27
  • Hey guys thanks for the help. I've figured out. The problem is that I'm using AngularFire2 and the `toPromise()` is not working correctly. I switched to the main Firebase SDK and works perfectly. Thank you – MrRobot Apr 23 '20 at 15:39

0 Answers0