Vue 封裝防刷新考試倒計(jì)時(shí)組件的實(shí)現(xiàn)
本文詳細(xì)的介紹了防刷新考試倒計(jì)時(shí)組件 ,分享給大家,也給自己留個(gè)筆記,感興趣的可以了解下

<!-- 考試倒計(jì)時(shí)組件 -->
<template>
<div class="time">
<p>00:{{timerCount2}}:{{timerCount1}}</p>
<button @click="reset">重新計(jì)時(shí)</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;
},
// 重新計(jì)時(shí)
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);
},
// 倒計(jì)時(shí)
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); //存儲(chǔ)秒
}
if (startTime2) {
this.timeMinutes = startTime2;
} else {
this.timeMinutes = this.minutes;
localStorage.setItem("startTime2", this.minutes); //存儲(chǔ)分
}
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("考試時(shí)間到");
} 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() {
// 退出后清除計(jì)時(shí)器
if (this.timer) {
clearInterval(this.timer);
}
}
};
</script>
<style scoped>
.time {
color: #f72a3a;
font-weight: bold;
font-size: 26px;
}
</style>
到此這篇關(guān)于Vue 封裝防刷新考試倒計(jì)時(shí)組件的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue 防刷新考試倒計(jì)時(shí)組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vueJS簡(jiǎn)單的點(diǎn)擊顯示與隱藏的效果【實(shí)現(xiàn)代碼】
下面小編就為大家?guī)硪黄獀ueJS簡(jiǎn)單的點(diǎn)擊顯示與隱藏的效果【實(shí)現(xiàn)代碼】。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,一起跟隨小編過來看看吧2016-05-05
vue.js實(shí)現(xiàn)點(diǎn)擊圖標(biāo)放大離開時(shí)縮小的代碼
這篇文章主要介紹了vue.js實(shí)現(xiàn)點(diǎn)擊圖標(biāo)放大離開時(shí)縮小,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Vue中動(dòng)態(tài)綁定Ref的兩種方式總結(jié)
Vue3中的ref是一種創(chuàng)建響應(yīng)式引用的方式,它在Vue生態(tài)系統(tǒng)中扮演著重要角色,下面這篇文章主要給大家介紹了關(guān)于Vue中動(dòng)態(tài)綁定Ref的兩種方式,需要的朋友可以參考下2024-09-09
vue項(xiàng)目打包后提交到git上為什么沒有dist這個(gè)文件的解決方法
這篇文章主要介紹了vue項(xiàng)目打包后提交到git上為什么沒有dist這個(gè)文件的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
在移動(dòng)端使用vue-router和keep-alive的方法示例
這篇文章主要介紹了在移動(dòng)端使用vue-router和keep-alive的方法示例,vue-router與keep-alive提供的路由體驗(yàn)與移動(dòng)端是有一定差別的,感興趣的小伙伴們可以參考一下2018-12-12
vue項(xiàng)目使用高德地圖時(shí)報(bào)錯(cuò):AMap?is?not?defined解決辦法
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目使用高德地圖時(shí)報(bào)錯(cuò):AMap?is?not?defined的解決辦法,"AMap is not defined"是一個(gè)錯(cuò)誤提示,意思是在代碼中沒有找到定義的AMap,需要的朋友可以參考下2023-12-12
ElementUI中el-dropdown-item點(diǎn)擊事件無效問題
這篇文章主要介紹了ElementUI中el-dropdown-item點(diǎn)擊事件無效問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
vue2.0使用v-for循環(huán)制作多級(jí)嵌套菜單欄
這篇文章主要介紹了vue2.0制作多級(jí)嵌套菜單欄,主要使用v-for循環(huán)生成一個(gè)多級(jí)嵌套菜單欄,這個(gè)方法應(yīng)用非常廣泛,需要的朋友可以參考下2018-06-06

