Set timeout in alert in Ionic2


#1

I’ve created an alert message which I want to close after a set defined time only. Below is my code:

 showAlert() {
      let alert = this.alertCtrl.create({       
        subTitle: 'The information you have provided is incomplete or invalid. Please check your entries and check again.'             
      });
      alert.present();
    }

showAlert() is the method that will be invoked after an event. Now, I want to set timeout for it but I couldn’t get any solution for it.


#2

Have you tried:

 showAlert() {
      let alert = this.alertCtrl.create({       
        subTitle: 'The information you have provided is incomplete or invalid. Please check your entries and check again.'             
      });
      alert.present();
      setTimeout(()=>{
          alert.dismiss();
      }, SOME_TIMEOUT_IN_MILISECONDS);
    }

setting the SOME_TIMEOUT_IN_MILISECONDS to some value in miliseconds ofc.


#3

Thanks a lot, it worked.


#4

Why are you using an alert here? This seems to be what toasts were invented for.


#5

That didn’t hit my mind. I’ll try it now. Thanks for the advice