在警报按钮上单击停止计数器

时间:2017-10-29 21:38:41

标签: angular ionic2

我有这个代码,我希望它在用户点击警报消息中的是/否时停止倒计时。我是离子的新人,所以我很感激帮助!谢谢!

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

1 个答案:

答案 0 :(得分:1)

在两个按钮的处理程序中,调用clearInterval传递计时器以停止计时器。

handler: () => {
            console.log('NO, I am Okay');
            clearInterval(this.timer) 
          }