Vue 封装防刷新考试倒计时组件的实现

 更新时间:2020年06月05日 09:22:43   作者:Vam的金豆之路  
这篇文章主要介绍了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>

到此这篇关于Vue 封装防刷新考试倒计时组件的实现的文章就介绍到这了,更多相关Vue 防刷新考试倒计时组件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue.set与this.$set的用法与使用场景介绍

    Vue.set与this.$set的用法与使用场景介绍

    Vue.set()和this.$set()这两个api的实现原理基本一模一样,都是使用了set函数,下面这篇文章主要给大家介绍了关于Vue.set与this.$set的用法与使用场景,需要的朋友可以参考下
    2022-03-03
  • 使用Vue Composition API写出清晰、可扩展的表单实现

    使用Vue Composition API写出清晰、可扩展的表单实现

    这篇文章主要介绍了使用Vue Composition API写出清晰、可扩展的表单实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • 详解Vue实现直播功能

    详解Vue实现直播功能

    这篇文章主要介绍了Vue实现直播功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-10-10
  • vue使用H5的audio标签问题

    vue使用H5的audio标签问题

    这篇文章主要介绍了vue使用H5的audio标签问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • vue debug 二种方法

    vue debug 二种方法

    这篇文章主要介绍了vue debug 二种方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-09-09
  • vue源码入口文件分析(推荐)

    vue源码入口文件分析(推荐)

    这篇文章主要介绍了vue源码入口文件分析(推荐),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • 概述VUE2.0不可忽视的很多变化

    概述VUE2.0不可忽视的很多变化

    本文给大家分析下vue2.0几个重要的与自己目前项目相关的变化,也是vue2.0不可忽视的变化,非常不错,具有参考借鉴价值,感兴趣的朋友一起看看吧
    2016-09-09
  • nuxt.js服务端渲染中axios和proxy代理的配置操作

    nuxt.js服务端渲染中axios和proxy代理的配置操作

    这篇文章主要介绍了nuxt.js服务端渲染中axios和proxy代理的配置操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • vue使用Print.js打印页面样式不出现的解决

    vue使用Print.js打印页面样式不出现的解决

    这篇文章主要介绍了vue使用Print.js打印页面样式不出现的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • vue中如何更改element-ui主题色

    vue中如何更改element-ui主题色

    这篇文章主要介绍了vue中如何更改element-ui主题色,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08

最新评论