Alert- if do not press alert button go to a page

#1

Is there a way to send a person to a page etc if alert button(s) are not pressed after a certain timeout.

I know how to send people to a page if they do press the buttons but want to send people people to a page if they do not press alert button after a certain time.

#2

Hi @lunneyd,

Maybe you can use a setTimeout() with a function that navigates to the page right after/before you show the alert, and only cancel it if the button is pressed. Like:

const timeout = setTimeout(() => this.navigateToPage(), 10000);

And if the button is pressed, just cancel the timeout with clearTimeout(timeout);.

Hope it helps,
Rodrigo

1 Like
#3

Thanks @FdezRomero that works but the alert still appears when it goes to the next page. Is there anyway when it goes to the specific page it dismiss the alert.

I tried the below code but the alert disappears instantly but I would like it to only disappear if it goes to the specific page

alert.present();
 var timeout = setTimeout(() => this.navCtrl.push('SpecificPage'), 6000);
 alert.dismiss();
#4

If I understood it correctly, you want to dismiss the alert and cancel the timeout when the alert button is pressed, right? If so, you have to call clearTimeout(timeout); inside the button’s handler function. If you’re using Ionic 3:

let timeout;

const alert = alertController.create({
  header: 'Alert',
  subHeader: 'Subtitle',
  message: 'This is an alert message.',
  buttons: [{
    text: 'OK',
    handler: () => clearTimeout(timeout);
  }]
});

await alert.present();

timeout = setTimeout(() => {
  alert.dismiss();
  this.navCtrl.push('SpecificPage');
}, 6000);
1 Like
#5

Currently what is happening is when the button is not pressed it goes to the new page which I want but the alert is still on the new page, I do not want this part.

I want the alert to be removed from the new page when it goes on to it.

#6

I see. Just call alert.dismiss() before navigating to the new page. I have updated the example above.