vue 倒计时

 <div class="top">
    <div class="delay">剩余时间</div>
     <div class="time">{{ countdownText }}</div>
     <div v-if="isLastTenMinutes" style="color:red;margin:10px 5%;">
     注意:距离考试结束还有不到10分钟,请尽快完成答题!
</div>
countdownText: "",
isLastTenMinutes: false,
targetTimestamp: 0, // 添加默认值
countdownTimer: null, // 定时器ID

 updateCountdown() {
      setTimeout(() => {
        this.$API
          .getExamCountDown({ examId: this.params.examId })
          .then((res) => {
            // 获取到的时间戳
            this.targetTimestamp = res;

            // 清空之前的定时器
            clearInterval(this.countdownTimer);

            // 更新倒计时
            this.countdownTimer = setInterval(() => {
              const now = Date.now();
              const remainingTime = this.targetTimestamp - now;

              if (remainingTime <= 0) {
                // 时间到了,执行相应操作
                // clearInterval(this.countdownTimer);
                this.$API.submitPaper(this.resDataParams).then((res) => {
                  if (res) {
                    this.$message({
                      type: "success",
                      message: "交卷成功!",
                    });
                    this.$router.push({ path: "/exam",query: { fullscreen: false },});
                    window.location.reload()
                  }
                });
              } else {
                const hours = Math.floor(remainingTime / 1000 / 60 / 60);
                const minutes = Math.floor(remainingTime / 1000 / 60);
                const seconds = Math.floor((remainingTime / 1000) % 60);

                const formattedHours = String(hours).padStart(2, "0");
                // 将剩余的分钟数转换为小于60的数值,分别获取小时数和不足一小时的分钟数
                const formattedMinutes = String(Math.floor(minutes % 60)).padStart(2, "0");
                const formattedSeconds = String(Math.floor(seconds % 60)).padStart(2, "0");
                this.countdownText = `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;

                if (remainingTime <= 10 * 60 * 1000) {
                  this.isLastTenMinutes = true;
                }
              }
            }, 1000);
          });
      }, 1000);
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值