<!-- 考试倒计时组件 -->
<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() {
// 退出后清除计时器

更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/119134934

Vue回炉重造之封装防刷新考试倒计时组件的更多相关文章

  1. Vue回炉重造之封装一个实用的人脸识别组件

    前言 人脸识别技术现在越来越火,那么我们今天教大家实现一个人脸识别组件. 资源 element UI Vue.js tracking-min.js face-min.js 源码 由于我们的电脑有的有摄 ...

  2. Vue回炉重造之三次封装axios

    源码目录 在src目录下建立一个request文件夹.里面建立两个文件: http.js api.js 源码内容 http.js import axios from 'axios' // 引入axio ...

  3. Vue回炉重造之图片加载性能优化

    前言 图片加载优化对于一个网站性能好坏起着至关重要的作用.所以我们使用Vue来操作一波.备注 以下的优化一.优化二栏目都是我自己封装在Vue的工具函数里,所以请认真看完,要不然直接复制的话,容易出错的 ...

  4. Vue回炉重造之如何使用props、emit实现自定义双向绑定

    下面我将使用Vue自带的属性实现简单的双向绑定. 下面的例子就是利用了父组件传给子组件(在子组件定义props属性,在父组件的子组件上绑定属性),子组件传给父组件(在子组件使用$emit()属性定义一 ...

  5. Vue回炉重造之router路由(更新中)

    安装vue-router npm i vue-router -S 配置1.创建文件夹与文件 创建一个router文件夹,在文件夹中创建两个文件,分别是router.js和routes.js文件.2.编 ...

  6. Vue回炉重造之搭建考试答卷系统

    本篇章主要讲述系统搭建逻辑,有疑问的可以加微信联系我.考试系统 资源 Vue.js Element UI 第三方数据接口 业务 答题过程中,防止用户中途退出或者其他不可抗力因素阻碍答题,在每次选择都要 ...

  7. Asp.Net SignalR 使用记录 技术回炉重造-总纲 动态类型dynamic转换为特定类型T的方案 通过对象方法获取委托_C#反射获取委托_ .net core入门-跨域访问配置

    Asp.Net SignalR 使用记录   工作上遇到一个推送消息的功能的实现.本着面向百度编程的思想.网上百度了一大堆.主要的实现方式是原生的WebSocket,和SignalR,再次写一个关于A ...

  8. 《回炉重造 Java 基础》——集合(容器)

    整体框架 绿色代表接口/抽象类:蓝色代表类. 主要由两大接口组成,一个是「Collection」接口,另一个是「Map」接口. 前言 以前刚开始学习「集合」的时候,由于没有好好预习,也没有学好基础知识 ...

  9. 回炉重造之重读Windows核心编程-006-线程

    线程也是有两部分组成的: 线程的内核对象,操作系统用来管理线程和统计线程信息的地方. 线程堆栈,用于维护现场在执行代码的时候用到的所有函数参数和局部变量. 进程是线程的容器,如果进程中有一个以上的线程 ...

随机推荐

  1. sqlplus文件查看自带oracle命令的执行过程

    问题描述:看到一篇文章 在$ORACLE_HOME/bin/sqlplus中可以查看到数据库命令的查询语句.可以直接编辑sqlplus文件,查到到我们平时标准系统命令的原脚本,但是自己进行编辑查看却是 ...

  2. NodeJs学习日报——day3

    // 导入模块 const http = require('http') // 创建web服务器实例 const server = http.createServer() // 为服务器实例绑定req ...

  3. vue 滚动条样式设置

      App.vue 文件下加入下面css   // 滚动条宽度 ::-webkit-scrollbar{   width: 6px; } /* 定义滚动条轨道 */ ::-webkit-scrollb ...

  4. go源码阅读 - sync/mutex

    Mutex是go标准库中的互斥锁,用于处理并发场景下共享资源的访问冲突问题. 1. Mutex定义: // A Mutex is a mutual exclusion lock. // The zer ...

  5. pgpool-II 4.3 中文手册-前言

    什么是 Pgpool-II? Pgpool II 管理一个 PostgreSQL 服务器池,以实现单个 PostgreSQL 安装无法实现的一些功能.这些功能包括: 高可用 Pgpool-II 通过使 ...

  6. XCTF练习题---MISC---3-11

    XCTF练习题---MISC---3-11 flag:FLAG{LSB_i5_SO_EASY} 解题思路: 1.观察题目,下载附件 2.下载后是一张图片,根据习惯直接Stegsolve打开查看 3.通 ...

  7. 用ffmpeg对视频进行处理

    下载安装配置教程:传送门 关键步骤Windows: 官网 合并音频和视频 with open('video/x111.mp4','wb') as f: f.write(data_30080) with ...

  8. STM8S103F3P6 开发环境笔记

    STM8S103F3 产品手册 https://www.st.com/resource/en/datasheet/stm8s103f2.pdf 内核 16 MHz advanced STM8 core ...

  9. ucore lab8 文件系统 学习笔记

    最后一战果然过瘾.代码量够多,新机制够复杂度,都管饱.做这一课就像从高山上往下走,坡急且路险,还不知自己的方位,琢磨不透系统的架构.待到下了山,回头一看豁然开朗,原来方才自己所下的山是这般模样.在这里 ...

  10. Java实用类

    //String类常用方法 public int length()//获取String对象的字符序列的长度 n=s.length(); public boolean equals(String s)/ ...