On alert button click stop counter


#1

Hello all, i have this code and i want it to stop the countdown when the user clicks yes/ no

ts :

   let alert = this.alertCtrl.create({
        title: 'EMERGENCY ALERT!!!',
        subTitle: 'Call Emergency Contact ?!',
              buttons: [
    {
      text: 'NO, I am Okay',
      handler: () => {
          console.log('NO, I am Okay');
      }
    },
    {
      text: 'YES Call',
      handler: () => {
          console.log('YES Call');
      }
    }
              ],
              enableBackdropDismiss: false
    });
   alert.present();

   this.counter = 10;
   window.clearInterval(this.timer);
   this.timer = setInterval(() => {
       this.counter--;
       if (this.counter === 0) {
           window.clearInterval(this.timer);

           /*After counter value is 0 means 10000 is completed */
           this.startFilling();
       }
   }, 1000);
}

startFilling() {
    console.log(true);
}

#2

You should call the function to stop your counter in the handler of both buttons.


#3

thank you for replying so fast ! do you mind giving me an example on how to write it?
this is my first time using ionic and i’m still figuring out the syntax. Thank you!


#4

Is the code to stop the counter already written or not?


#5

yes i used this to stop the counter

 clearInterval(this.timer);

and it worked thank you!


#6

i do still have one Question if it’s okay

how can I write this in the correct syntax

 let alert = this.alertCtrl.create({
        title: 'EMERGENCY ALERT!!!',
        subTitle: 'Call Emergency Contact ?! Countdown {{counter}}',