一、Vue實現倒計時
在Vue中實現倒計時功能,一般使用Vue的計算屬性來實現。採用計算屬性,可以有效避免在數據改變後的重複渲染,提高代碼的效率。
<template>
<div>
<p>{{ countDownTime }}</p>
</div>
</template>
<script>
export default {
data() {
return {
startTime: 60 // 自定義開始時間
}
},
computed: {
countDownTime() {
setInterval(() => {
this.startTime--;
}, 1000)
return this.startTime;
}
}
}
</script>
上述代碼中,我們在計算屬性中利用setInterval來實現倒計時功能。同時,我們設置了開始時間startTime為60秒,在每次執行計算屬性時,startTime會減1直到0秒為止。
二、Vue實現倒計時組件
如果我們需要在多個頁面、多個組件中復用倒計時功能,這時我們需要創建一個Vue倒計時組件。下面是一個簡單的Vue倒計時組件:
<template>
<div>
<p>{{ countDownTime }}</p>
</div>
</template>
<script>
export default {
name: 'count-down',
props: {
time: {
type: Number,
default: 60 // 默認時間60秒
}
},
data() {
return {
startTime: this.time
}
},
computed: {
countDownTime() {
setInterval(() => {
this.startTime--;
}, 1000)
return this.startTime;
}
}
}
</script>
這個倒計時組件包含一個計算屬性countDownTime,用於計算倒計時時間;同時接收一個props參數time,用於設定倒計時時間。使用該組件時,只需要在父組件中引入該組件並傳入倒計時時間即可。
三、Vue驗證碼倒計時
驗證碼倒計時可以在簡訊驗證、郵件驗證、註冊等場景中使用。對於驗證碼倒計時,我們需要的是一段能夠倒計時的時間,並且在時間結束後,重新獲取驗證碼。下面是一個Vue實現的驗證碼倒計時組件:
<template>
<div>
<p v-if="!isCount"><a href="javascript:;" @click="startCountDown">獲取驗證碼</a></p>
<p v-else>{{ countDownTime }} 秒後重新獲取</p>
</div>
</template>
<script>
export default {
name: 'code-count-down',
data() {
return {
countDownTime: 60, // 默認時間60秒
isCount: false
}
},
methods: {
startCountDown() {
this.isCount = true;
const timer = setInterval(() => {
this.countDownTime--;
if (this.countDownTime === 0) {
clearInterval(timer);
this.countDownTime = 60;
this.isCount = false;
}
}, 1000)
}
}
}
</script>
以上的代碼中,我們通過使用v-if和v-else對計時狀態進行了判斷。如果計時沒有開始,則顯示獲取驗證碼按鈕;如果計時開始了,則顯示倒計時時間,並且倒計時時間結束後,重新顯示獲取驗證碼按鈕。在組件中,我們使用了一個isCount變數來記錄倒計時狀態,使用countDownTime來記錄時間狀態。
四、Vue組件里的倒計時定時器
在Vue中使用定時器來實現倒計時功能,需要注意一些細節問題。下面是在Vue組件中使用定時器時的一些注意事項:
1. 避免多次開啟倒計時
在設置定時器時,需要在使用定時器前將定時器清空,否則將會出現多個定時器同時執行的情況,導致數據混亂。
startTimer() {
if (!this.timer) { // 判斷定時器是否存在,避免多次開啟定時器
this.timer = setInterval(() => {
this.countDownTime--;
if (this.countDownTime === 0) {
this.stopTimer(); // 定時器結束後,及時清除定時器
}
}, 1000)
}
},
stopTimer() {
clearInterval(this.timer);
this.timer = null; // 清空定時器
}
2. 避免Vue組件銷毀後,定時器還在運行
在Vue組件銷毀後,需要及時清除定時器,否則將成為內存泄漏的原因。
beforeDestroy() {
clearInterval(this.timer);
this.timer = null; // 清空定時器
}
3. 避免數據混亂
在倒計時組件中,如果使用了props參數,需要使用Vue的watch監聽props參數變化,避免出現數據混亂。
watch: {
time(newVal, oldVal) {
if (newVal !== oldVal) {
this.countDownTime = newVal;
}
}
}
五、Vue倒計時功能
Vue倒計時功能是一個非常常用的功能,一般用於統計時間,倒計時功能等場景。下面是一個Vue倒計時功能的組件:
<template>
<div>
<p>{{ countDownTime }}</p>
</div>
</template>
<script>
export default {
name: 'vue-count-down',
props: {
targetTime: {
type: Number,
default: 1609459200 // 默認時間為2021/1/1 00:00:00
}
},
data() {
return {
remainingTime: ''
}
},
mounted() {
this.init();
},
methods: {
init() {
setInterval(() => {
this.remainingTime = this.getRemainingTime();
}, 1000)
},
getRemainingTime() {
let targetTime = new Date(this.targetTime * 1000).getTime();
let currentTime = new Date().getTime();
let remainingTime = targetTime - currentTime;
let seconds = Math.floor(remainingTime / 1000 % 60);
let minutes = Math.floor(remainingTime / 1000 / 60 % 60);
let hours = Math.floor(remainingTime / 1000 / 60 / 60 % 24);
let days = Math.floor(remainingTime / 1000 / 60 / 60 / 24);
return `${days}天${hours}小時${minutes}分${seconds}秒`;
}
}
}
</script>
該組件通過傳入目標時間(時間戳),計算與當前時間的時間差,並通過計算獲得剩餘時間(天、小時、分鐘、秒),並且每秒更新一次數據。使用該組件時,只需要傳入目標時間戳即可。
六、Vue倒計時60秒
Vue倒計時60秒功能常用於一些簡單的倒計時場景,例如:重新獲取驗證碼等。下面是一個簡單的Vue倒計時60秒組件:
<template>
<div>
<p>{{ countDownTime }}</p>
</div>
</template>
<script>
export default {
name: 'vue-count-down-60s',
data() {
return {
countDownTime: 60 // 默認時間為60秒
}
},
mounted() {
setInterval(() => {
this.countDownTime--;
}, 1000)
}
}
</script>
該倒計時組件使用了一個計算屬性countDownTime,每隔一秒鐘減一,直到倒計時結束。
七、Vue寫倒計時一分鐘
Vue寫倒計時一分鐘功能是一種非常簡單的、易於實現的倒計時功能。下面是該種情況下的Vue倒計時組件:
<template>
<div>
<p>{{ countDownTime }}</p>
</div>
</template>
<script>
export default {
name: 'count-down-one-minute',
data() {
return {
countDownTime: 60 // 默認時間為60秒
}
},
mounted() {
setInterval(() => {
this.countDownTime--;
}, 1000)
}
}
</script>
該倒計時組件與Vue倒計時60秒類似,都是採用了計算屬性來計算時間,並且每隔一秒鐘減一,直到倒計時結束的方式來實現。
八、Vue實現驗證碼倒計時功能
驗證碼倒計時功能在之前的Vue驗證碼倒計時中有所涉及,本節中,我們將深入討論Vue實現驗證碼倒計時功能的細節問題。
1. 定義狀態變數
在Vue組件中,我們需要定義一個狀態變數來控制驗證碼倒計時狀態。該狀態變數將用於記錄倒計時的開始、暫停、重新開始等狀態。
data() {
return {
countDownTime: 60, // 默認時間為60秒
isCounting: false, // 是否正在倒計時
timer: null // 定時器
}
}
2. 開始倒計時
當用戶點擊獲取驗證碼按鈕時,將啟動倒計時。在啟動倒計時前,需要判斷定時器是否存在,如果不存在,才可以開啟定時器。同時,我們需要使用isCounting變數來記錄倒計時的狀態。
startCountDown() {
if (!this.timer) { // 判斷定時器是否存在,避免多次開啟定時器
this.isCounting = true;
this.timer = setInterval(() => {
this.countDownTime--;
if (this.countDownTime === 0) {
this.stopCountDown(); // 停止倒計時
}
}, 1000)
}
}
3. 停止倒計時
在倒計時結束時,需要關閉定時器。同時,我們需要將狀態變數isCounting設置為false,此時用戶可以重新獲取驗證碼。
stopCountDown() {
clearInterval(this.timer);
this.isCounting = false;
this.timer = null; // 清空定時器
this.countDownTime = 60; // 重新設置倒計時時間
}
4. 監聽props參數變化
如果我們在倒計時組件中使用props參數來定義倒計時時間,需要使用Vue的watch監聽props參數變化,並且在props參數變化時,重新設置倒計時時間。
watch: {
time(newVal, oldVal) {
if (newVal !== oldVal) {
this.countDownTime = newVal;
}
}
}
九、Vue實現倒計時功能總結
通過上述的講解,我們可以看到,Vue實現倒計時功能非常簡單,只需要運用Vue的計算屬性、props參數、定時器等特性,即可輕鬆實現一項複雜倒計時功能。</p
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/192454.html