0
点赞
收藏
分享

微信扫一扫

vue使用setInterval完成倒计时,带暂停开始

kolibreath 2022-02-08 阅读 52
	//倒计时方法
    countdown(){
      if(this.maxTime>0){//最大时间大于0
        this.maxTime--
        this.minutes=Math.floor(this.maxTime / 60).toString().padStart(2,'0')
        this.seconds=Math.floor(this.maxTime % 60).toString().padStart(2,'0')
      }else{
        clearInterval(this.timers);
      }
    },
    //开始倒计时
    startCountdown(){
      this.maxTime=this.timeValue*60;//最大时间赋值
      this.timers=setInterval(()=>{
        this.countdown()
      },1000)
    },
    //终止倒计时
    closeTime(){
      this.timeValue=0
      this.minutes="00";
      this.seconds="00";
      this.maxTime=''
      clearInterval(this.timers);
    },
    //暂停开始
    stopTime(){
      if(this.stopText==="暂停"){
        clearInterval(this.timers);
      }else if(this.stopText==="开始"){
        this.timers=setInterval(()=>{
          this.countdown()
        },1000)
      }
      this.stopText=this.stopText==="开始"?"暂停":"开始"
    },
举报

相关推荐

0 条评论