指定时间点之后15分钟内倒计时

startCountdown() {
      if (this.countdown > 0) {
        this.timer = setInterval(() => {
          if (this.countdown > 0) {
            this.countdown--;
            this.getTimeFn();
          } else {
            this.stopCountdown();
          }
        }, 1000);
      }
    },
    getTimeFn() {
      let leftTime = this.countdown;
      var d, h, m, s;
      d = Math.floor(leftTime / 60 / 60 / 24);
      h = Math.floor((leftTime / 60 / 60) % 24);
      m = Math.floor((leftTime / 60) % 60);
      s = Math.floor(leftTime % 60);
      // ms = Math.floor(leftTime % 1000);
      // ms = ms < 100 ? "" + ms : ms;
      s = s < 10 ? "" + s : s;
      m = m < 10 ? "" + m : m;
      h = h < 10 ? "" + h : h;
      // console.log(d + "天", h + "小时", m + "分", s + "秒");
      this.timStr =
        (d > 0 ? d + "天" : "") +
        (h > 0 ? h + "小时" : "") +
        (m > 0 ? m + "分" : "") +
        (s > 0 ? s + "秒" : "");
    },

    stopCountdown() {
      if (this.timer) {
        this.showFooterBtn = true;
        this.startFlowFn();
        clearInterval(this.timer);
        this.timer = null;
      }
    },
    // 首页代办进来需判断
    beforStartFn() {
      if (!this.flowInfo.openObservationFlag) {
        return;
      } else {
        const targetTimeBase = new Date("2025-03-05 18:22:00");
        targetTimeBase.setMinutes(targetTimeBase.getMinutes() + 15);
        let targetTimePlus15Minutes = targetTimeBase;
        let targetTime = new Date(targetTimePlus15Minutes).getTime();
        const now = new Date().getTime();
        const timeDifference = targetTime - now;
        // const timeDifference = this.flowInfo.openObservationTime;
        if (timeDifference > 0) {
          this.countdown = Math.ceil(timeDifference / 1000);
          this.startCountdown();
        } else {
          this.showFooterBtn = true;
          this.startFlowFn();
          // this.countdown = 0;
        }
      }
    },
      countdown:"", // 秒数
      timer: null,
      timStr: "",

 

posted @ 2025-03-06 09:14  ThisCall  阅读(67)  评论(0)    收藏  举报