I'm currently developing an exam app and I'm facing an issue where the view controller is getting stuck in a loop after unsubscribing from the timer. The goal is to notify the user when their time is up and redirect them to a results page.
If anyone has any suggestions or solutions for this problem, please let me know!
Here's the code snippet:
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();
}