vue页面的定时刷新setInterval()

setInterval() 方法
可按照指定的周期(以毫秒计)不停地调用函数或表达式,直到 clearInterval() 被调用或窗口被关闭。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的参数。

注:定时器的this是指向 window的

提示: 如果你只想执行一次可以使用 setTimeout() 方法。

  <el-button @click="IntervalShowButton">倒计时</el-button>
  <el-button @click="IntervalDestroyed">销毁</el-button>
  
    data(){
    return{
      apnnn: null
    }
  },

      IntervalDestroyed(){
      clearInterval(this.apnnn);
    },
    IntervalShowButton(){
      var maxtime = 5; //秒
      let _that = this;
      this.apnnn =setInterval(()=>{
        if (maxtime >= 0) {
          console.log('maxtime'+maxtime)
          --maxtime;

        }else{
          clearInterval(_that.apnnn);
          console.log('clearInterval 销毁')
          // alert("时间到,结束!");
        }
      }, 1000)
    },
  
posted @ 2022-01-05 14:19  寒冷的雨呢  阅读(780)  评论(0编辑  收藏  举报