3
votes

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
I want to set timeout for it you mean dismiss after an amount of time..? or create after some amount of time? - Suraj Rao

2 Answers

6
votes

If you want to use timeout to invoke alert,

you can use the global setTimeout() function like so:

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

}

In case you want to dismiss after timeout,

setTimeout(()=>alert.dismiss(),3000);
0
votes

Instead of using alert , prefer using toast for such issues , you can display it for as much time you want.

for using toast , you may proceed like mentioned below:

import {Toast} from 'ionic-native';
     Toast.show("The information you have provided is incomplete or invalid. Please check your entries and check again.", '3000', 'center').subscribe(
            toast => {
              console.log(toast);
            }
          );

"3000" : is the time for which you want to display , timing is in milliseconds , hence , 3000 = 3 sec. "center": is the position of the toast , it can either be top , center , or bottom.