Vue 封裝防刷新考試倒計時組件的實現(xiàn)
更新時間:2020年06月05日 09:22:43 作者:Vam的金豆之路
這篇文章主要介紹了Vue 封裝防刷新考試倒計時組件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
本文詳細的介紹了防刷新考試倒計時組件 ,分享給大家,也給自己留個筆記,感興趣的可以了解下

<!-- 考試倒計時組件 -->
<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 封裝防刷新考試倒計時組件的實現(xiàn)的文章就介紹到這了,更多相關Vue 防刷新考試倒計時組件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vueJS簡單的點擊顯示與隱藏的效果【實現(xiàn)代碼】
下面小編就為大家?guī)硪黄獀ueJS簡單的點擊顯示與隱藏的效果【實現(xiàn)代碼】。小編覺得挺不錯的,現(xiàn)在分享給大家,一起跟隨小編過來看看吧2016-05-05
vue項目打包后提交到git上為什么沒有dist這個文件的解決方法
這篇文章主要介紹了vue項目打包后提交到git上為什么沒有dist這個文件的解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
在移動端使用vue-router和keep-alive的方法示例
這篇文章主要介紹了在移動端使用vue-router和keep-alive的方法示例,vue-router與keep-alive提供的路由體驗與移動端是有一定差別的,感興趣的小伙伴們可以參考一下2018-12-12
vue項目使用高德地圖時報錯:AMap?is?not?defined解決辦法
這篇文章主要給大家介紹了關于vue項目使用高德地圖時報錯:AMap?is?not?defined的解決辦法,"AMap is not defined"是一個錯誤提示,意思是在代碼中沒有找到定義的AMap,需要的朋友可以參考下2023-12-12
ElementUI中el-dropdown-item點擊事件無效問題
這篇文章主要介紹了ElementUI中el-dropdown-item點擊事件無效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
vue2.0使用v-for循環(huán)制作多級嵌套菜單欄
這篇文章主要介紹了vue2.0制作多級嵌套菜單欄,主要使用v-for循環(huán)生成一個多級嵌套菜單欄,這個方法應用非常廣泛,需要的朋友可以參考下2018-06-06

