Vue remakes the package anti-refresh test countdown component

Vue remakes the package anti-refresh test countdown component

Welcome to read this blog post. This blog post mainly describes [Vue remade package anti-refresh test countdown component], the text is easy to understand, if there is anything wrong, please correct me.

<!--   -->
<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();
          alrt(" ");
        } 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>
 

Thank you for reading. If you have any feelings, please give me a thumbs up and close a note!