Set timeout in alert in Ionic2

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.

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.

1 Like

Thanks a lot, it worked.

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

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