Window setInterval() 方法和clearInterval() 方法

定义和用法

setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。

setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的参数。

提示: 1000 毫秒= 1 秒。

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

    //倒计时
    getDisTime() {
      if (this.countDown > 0) {
        let mimuteDiff = parseInt(this.countDown / 60);
        let secondDiff = parseInt(this.countDown % 60);
        mimuteDiff = mimuteDiff >= 10 ? mimuteDiff : "0" + mimuteDiff;
        secondDiff = secondDiff >= 10 ? secondDiff : "0" + secondDiff;
        this.seckillName = mimuteDiff + ":" + secondDiff;
        this.countDown = this.countDown - 1;
      } else {
        this.resetSeckillTime();
      }
    },
    //倒计时点击倒计时
    seckillTime() {
      if (!this.timer) {
        clearInterval(this.timer);
        this.timer = setInterval(() => {
          this.getDisTime();
        }, 1000);
      } else {
        clearInterval(this.timer);
        this.timer = "";
      }
    },
posted @ 2022-05-12 16:39  不特别但唯一  阅读(75)  评论(0)    收藏  举报