vue定時(shí)器設(shè)置和關(guān)閉頁(yè)面時(shí)關(guān)閉定時(shí)器方式
vue定時(shí)器設(shè)置和關(guān)閉頁(yè)面時(shí)關(guān)閉定時(shí)器
我看了別人寫的博客關(guān)于設(shè)置和清除定時(shí)器都比較復(fù)雜,我感覺(jué)其實(shí)很簡(jiǎn)單的幾行代碼就好。
把我的寫法記錄一下。
methods中
setTime() { //設(shè)置定時(shí)器
? this.clearTimeSet=setInterval(() => {
? ? this.webSocketClientOnopen();
? }, 1000);
},
clearTime() { //清除定時(shí)器
? clearInterval(this.clearTimeSet);
}mounted 中
mounted : { //頁(yè)面加載完畢就開(kāi)始加載定時(shí)器
? this.setTime();
}beforeDestroy() { ? ?//頁(yè)面關(guān)閉時(shí)清除定時(shí)器 ?
? clearInterval(this.clearTimeSet);
},還有在tab頁(yè)面加定時(shí)器和銷毀定時(shí)器
stro() { //主頁(yè)A
? ? ? ? ? ? this.timeClss = setInterval(() => {
? ? ? ? ? ? ? ? this.getFirstList()
? ? ? ? ? ? }, 5 * 100)
? ? ? ? },
? ? ? ? twosli() {// 主頁(yè)B
? ? ? ? ? ? this.times = ?setInterval(() => {
? ? ? ? ? ? ? ? this.getThirdList()
? ? ? ? ? ? }, 5 * 100)
? ? ? ? },
? ? ? ? clearTime() { // 主頁(yè)A 清除
? ? ? ? ? ? clearInterval(this.timeClss)
? ? ? ? },
? ? ? ? clearcloss() { // 主頁(yè)B 清除
? ? ? ? ? ? clearInterval(this.times)
? ? ? ? },?handleClick(tab, event) { // 每個(gè)tab點(diǎn)擊切換的函數(shù)方法
? ? ? ? ? ? if(tab.label == '主頁(yè)A'){
? ? ? ? ? ? ? ? this.getFirstList()
? ? ? ? ? ? ? ? this.stro()?
? ? ? ? ? ? ? ? this.clearcloss()
? ? ? ? ? ? }else if(tab.label == '工單A'){
? ? ? ? ? ? ? ? this.getSecList()
? ? ? ? ? ? ? ? this.clearTime()
? ? ? ? ? ? ? ? this.clearcloss()
? ? ? ? ? ? }else if(tab.label == '主頁(yè)B'){
? ? ? ? ? ? ? ? this.getThirdList()
? ? ? ? ? ? ? ? this.twosli()
? ? ? ? ? ? ? ? this.clearTime()
? ? ? ? ? ? }else if(tab.label == '工單B'){
? ? ? ? ? ? ? ? this.getFourList()
? ? ? ? ? ? ? ? this.clearcloss()
? ? ? ? ? ? ? ? this.clearTime()
? ? ? ? ? ? }
? ? ? ? }首先,把定時(shí)器和清除定時(shí)器的方法分別寫成函數(shù)方法,然后在handleClick方法中當(dāng)要切換tab的時(shí)候,在不需要的tab選項(xiàng)卡里調(diào)用定時(shí)器和清除定時(shí)器的方法就好了,這個(gè)就可以清除或設(shè)置定時(shí)器了!
vue定時(shí)器的使用全解
vue使用定時(shí)器
在vue中使用定時(shí)器,很多情況下,進(jìn)入和退出vue界面,都沒(méi)有清除定時(shí)器,從而導(dǎo)致有很多定時(shí)器一起工作,這樣肯定是不行的,接下來(lái)就使用當(dāng)用戶進(jìn)入界面時(shí)啟用定時(shí)器,當(dāng)用戶離開(kāi)當(dāng)前界面時(shí)就清除定時(shí)器。
代碼實(shí)現(xiàn)
<template>
</template>
<script>
import store from '@/store'
import Vue from 'vue'
export default {
name: "test",
data () {
return {
timer: null
}
},
methods: {
setTimer() {
if(this.timer == null) {
this.timer = setInterval( () => {
console.log('開(kāi)始定時(shí)...每過(guò)一秒執(zhí)行一次')
}, 1000)
}
}
},
created: function() {
this.getFamilyBase_info()
// 每次進(jìn)入界面時(shí),先清除之前的所有定時(shí)器,然后啟動(dòng)新的定時(shí)器
clearInterval(this.timer)
this.timer = null
this.setTimer()
},
destroyed: function () {
// 每次離開(kāi)當(dāng)前界面時(shí),清除定時(shí)器
clearInterval(this.timer)
this.timer = null
}
}
</script>
<style scoped>
</style>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue項(xiàng)目tween方法實(shí)現(xiàn)返回頂部的示例代碼
這篇文章主要介紹了vue項(xiàng)目tween方法實(shí)現(xiàn)返回頂部,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
vuejs+element UI點(diǎn)擊編輯表格某一行時(shí)獲取內(nèi)容填入表單的示例
這篇文章主要介紹了vuejs+element UI點(diǎn)擊編輯表格某一行時(shí)獲取內(nèi)容填入表單的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
基于Vue設(shè)計(jì)實(shí)現(xiàn)一個(gè)彈幕組件
這篇文章主要給大家分享一個(gè)開(kāi)發(fā)中常見(jiàn)的需求,接下來(lái)將為大家詳細(xì)介紹彈幕的實(shí)現(xiàn)以及設(shè)計(jì)思路一步一步描述出來(lái),希望大家能夠喜歡2023-06-06
如何通過(guò)Vue自定義指令實(shí)現(xiàn)前端埋點(diǎn)詳析
埋點(diǎn)分析是網(wǎng)站分析的一種常用的數(shù)據(jù)采集方法,下面這篇文章主要給大家介紹了關(guān)于如何通過(guò)Vue自定義指令實(shí)現(xiàn)前端埋點(diǎn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
Vue3后臺(tái)管理系統(tǒng)之創(chuàng)建和配置項(xiàng)目
后臺(tái)管理系統(tǒng)是我們?nèi)粘i_(kāi)發(fā)學(xué)習(xí)經(jīng)常遇到的一個(gè)項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于Vue3后臺(tái)管理系統(tǒng)之創(chuàng)建和配置項(xiàng)目的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
vue3如何利用自定義指令實(shí)現(xiàn)下拉框分頁(yè)懶加載
下拉框一開(kāi)始請(qǐng)求第一頁(yè)的內(nèi)容,滾動(dòng)到最后的時(shí)候,請(qǐng)求第二頁(yè)的內(nèi)容,如此反復(fù),直到所有數(shù)據(jù)加載完成,這篇文章主要介紹了vue3如何利用自定義指令實(shí)現(xiàn)下拉框分頁(yè)懶加載,需要的朋友可以參考下2024-07-07

