Vue回炉重造之封装防刷新考试倒计时组件

你好,我是Vam的金豆之路,可以叫我豆哥。2019年年度博客之星、技术领域博客专家。主要领域:前端开发。我的微信是 maomin9761,有什么疑问可以加我哦,自己创建了一个微信技术交流群,可以加我邀请你一起交流学习。最后自己也创建了一个微信公众号:前端历劫之路,欢迎关注。


欢迎阅读本博文,本博文主要讲述【Vue回炉重造之封装防刷新考试倒计时组件】,文字通俗易懂,如有不妥,还请多多指正。

在这里插入图片描述

<!-- 考试倒计时组件 -->
<template>
  <div class="time">
    <p>00:{{timerCount2}}:{{timerCount1}}</p>
    <button @click="reset">重新计时</button>
  </div>
</template>


<script>
export default {
  name: "Time",
  data() {
    return {
      timeSeconds: 0,
      timeMinutes: 0,
      seconds: 59, // 秒
      minutes: 1, // 分
      timer: null
    };
  },
  methods: {
    num(n) {
      return n < 10 ? "0" + n : "" + n;
    },
    // 重新计时
    reset() {
      sessionStorage.removeItem("answered");
      window.location.reload();
      localStorage.removeItem("startTime1");
      localStorage.removeItem("startTime2");
      clearInterval(this.timer);
    },
    // 清除
    clear() {
      localStorage.removeItem("startTime1");
      localStorage.removeItem("startTime2");
      sessionStorage.setItem("answered", 1);
      clearInterval(this.timer);
    },
    // 倒计时
    timing() {
      let [startTime1, startTime2] = [ localStorage.getItem("startTime1"), localStorage.getItem("startTime2") ];
      let nowTime = new Date().getTime();
      if (startTime1) {
        let surplus = this.seconds - parseInt((nowTime - startTime1) / 1000);
        this.timeSeconds = surplus <= 0 ? 0 : surplus;
      } else {
        this.timeSeconds = this.seconds;
        localStorage.setItem("startTime1", nowTime); //存储秒
      }
      if (startTime2) {
        this.timeMinutes = startTime2;
      } else {
        this.timeMinutes = this.minutes;
        localStorage.setItem("startTime2", this.minutes); //存储分
      }
      this.timer = setInterval(() => {
        if ( this.timeSeconds == 0 && this.timeMinutes != 0 && this.timeMinutes > 0 ) {
          let nowTime = new Date().getTime();
          this.timeSeconds = this.seconds;
          localStorage.setItem("startTime1", nowTime);
          this.timeMinutes--;
          localStorage.setItem("startTime2", this.timeMinutes);
        } else if (this.timeMinutes == 0 && this.timeSeconds == 0) {
          this.timeSeconds = 0;
          this.clear();
          alert("考试时间到");
        } else {
          this.timeSeconds--;
        }
      }, 1000);
    }
  },
  mounted() {
    if (sessionStorage.getItem("answered") != 1) {
      this.timing();
    }
  },
  computed: {
    timerCount1() {
      return this.timeSeconds < 10 ? "0" + this.timeSeconds : "" + this.timeSeconds;
    },
    timerCount2() {
      return this.timeMinutes < 10 ? "0" + this.timeMinutes : "" + this.timeMinutes;
    }
  },
  destroyed() {
    // 退出后清除计时器
    if (this.timer) {
      clearInterval(this.timer);
    }
  }
};
</script>
<style scoped>
.time {
  color: #f72a3a;
  font-weight: bold;
  font-size: 26px;
}
</style>

谢谢阅读,如果觉得有感触,麻烦帮忙点个赞,关个注吧!

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vam的金豆之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值