Set a timeout for warning in Ionic2

I have created a warning message that I want to close after the set time. 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();
}
      

Run codeHide result


showAlert () is the method that will be called after the event. Now I want to set a timeout for it, but I couldn't find any solution for this.

+3


source to share


2 answers


If you want to use a timeout to trigger an alert,

you can use a global function setTimeout()

like this:

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);

}

      



If you want to fire after a timeout,

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

      

+4


source


Instead of using an alert, prefer to use toast for such problems, you can display it for as long as you want.

to use toast, you can proceed as follows:



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": this is the time you want to display, the time in milliseconds, therefore 3000 = 3 seconds. "center": This is the position of the toast, it can be either top or bottom.

0


source







All Articles