1

I am creating an exam app so when time is up i needed a view controller to pop to tell the user he has exceeded his time and take him/her to a results page!!

But after unsubscribing from the timer, my function gets into a loop!!!

Please, i need help with this..

My codes below!

minutesDisplay: number = 0;
hoursDisplay: number = 0;
secondsDisplay: number = 0;

sub: Subscription;

showAlert() {
    let alert = this.alertCtrl.create({

      subTitle: 'Ooops, Time Up! ',
    });
    alert.present();
    this.activity.openModal();
  }

ngOnInit() {
    this.startTimer();
}

public ngOnDestroy() {
    if (this.minutesDisplay == 1){
        this.sub.unsubscribe();
    }

    return true;

}

private startTimer() {
    let timer = Observable.timer(1, 1000);
    this.sub = timer.subscribe(
        t => {
            this.ticks = t;
            this.secondsDisplay = this.getSeconds(this.ticks);
            this.minutesDisplay = this.getMinutes(this.ticks);
            this.hoursDisplay = this.getHours(this.ticks);
            this.ngOnDestroy();
        }
    ); 
    this.showAlert();
 }
2
  • Could you set a plunker, please?How do you see the loop? The alert shows all the time? Commented Aug 31, 2017 at 22:27
  • Yes, a plunker would be very helpful here. Otherwise we are just guessing at syntax. Commented Aug 31, 2017 at 22:33

2 Answers 2

1

I didn't try this ... but you could try the following:

public ngOnDestroy() {
    this.sub.unsubscribe();  // Stop the timer if the user navigates elsewhere
}

private startTimer() {
    let timer = Observable.timer(1, 1000);
    this.sub = timer.subscribe(
        t => {
            this.ticks = t;
            this.secondsDisplay = this.getSeconds(this.ticks);
            this.minutesDisplay = this.getMinutes(this.ticks);
            this.hoursDisplay = this.getHours(this.ticks);
            if (this.minutesDisplay == 1){
               this.sub.unsubscribe();  // Stop the timer when time is up.
               this.showAlert();        // Show the alert now
            }
        }
    ); 
 }
Sign up to request clarification or add additional context in comments.

10 Comments

I was thinking of the same :)
@DeborahK still returning same loop
You'll really need to provide a plunker for us to better examine how this is working. Go to this site: plnkr.co/edit/?p=catalogue Drop down the list from the big green New button and select 'Angular'. Then add your code to the generated files. If you can demonstrate the issue there ... we can work with the code to resolve it.
Also ... will this.minutesDisplay ever be 1? What does your getMinutes method do?
plnkr.co/edit/qk2c9nr6dBn2jWeaVf8d? thats the link to the code, you get to see what my getMInutes function does!!!
|
0
Observable.timer(1, 1000)
  .map(t=>this.getMinutes(t) === 1) // Observable<tick> -> Observable<boolean>
  .first(istimeUp => istimeUp) // take first Observable<true>
  .subscribe(()=>this.showAlert())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.