<!-- 考试倒计时组件 -->
<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

最新文章

  1. nginx实现单服务代理多域名
  2. 祝贺 Linux 25 岁:25 个关于 Linux 的惊人真相!【转载】
  3. sql 语句 事务
  4. css巧妙实现分隔线
  5. python---list列表
  6. Android UI系列-----时间、日期、Toasts和进度条Dialog
  7. 关于actionscript中新建一个sprite,设置大小(宽高)的问题。
  8. Uva 11754 Code Feat
  9. SQL SERVER NVARCHAR字段INSERT 中文乱码问题解决
  10. Android寒假实训云笔记总结——欢迎页
  11. NOI2014 起床困难综合症
  12. 在MVC5中的使用Ninject
  13. JDBC 连接数据库的步骤
  14. 学习Makefile
  15. java设定一个日期时间,加几分钟(小时或者天)后得到新的日期
  16. ArcGIS Server服务器监控
  17. 不能够连接到主机(名称为localhost)上的MySQL服务”
  18. PHP画矩形,椭圆,圆,画椭圆弧 ,饼状图
  19. 牛客训练三:处女座和小姐姐(三)(数位dp)
  20. 201709015工作日记--IntentService使用

热门文章

  1. 前端javascript之BOM、DOM操作、事件
  2. Figma禁封中国企业,下一个会是Postman吗?国产软件势在必行!
  3. 记录,element ui的日期选择器只有第一次回显成功
  4. .NET 7 Preview 3添加了这些增强功能
  5. python3-认识内置对象,运算符,表达式
  6. Java函数的学习
  7. OSPF MTU问题
  8. Bugku CTF练习题---MISC---眼见非实
  9. day02 真正的高并发还得看IO多路复用
  10. 快速了解Spring,简明Spring使用流程