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ù)雜,我感覺其實(shí)很簡(jiǎn)單的幾行代碼就好。
把我的寫法記錄一下。
methods中
setTime() { //設(shè)置定時(shí)器 ? this.clearTimeSet=setInterval(() => { ? ? this.webSocketClientOnopen(); ? }, 1000); }, clearTime() { //清除定時(shí)器 ? clearInterval(this.clearTimeSet); }
mounted 中
mounted : { //頁(yè)面加載完畢就開始加載定時(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界面,都沒有清除定時(shí)器,從而導(dǎo)致有很多定時(shí)器一起工作,這樣肯定是不行的,接下來就使用當(dāng)用戶進(jìn)入界面時(shí)啟用定時(shí)器,當(dāng)用戶離開當(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('開始定時(shí)...每過一秒執(zhí)行一次') }, 1000) } } }, created: function() { this.getFamilyBase_info() // 每次進(jìn)入界面時(shí),先清除之前的所有定時(shí)器,然后啟動(dòng)新的定時(shí)器 clearInterval(this.timer) this.timer = null this.setTimer() }, destroyed: function () { // 每次離開當(dāng)前界面時(shí),清除定時(shí)器 clearInterval(this.timer) this.timer = null } } </script> <style scoped> </style>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue實(shí)現(xiàn)實(shí)時(shí)刷新時(shí)間的功能
這篇文章主要為大家詳細(xì)介紹了如何Vue利用實(shí)現(xiàn)實(shí)時(shí)刷新時(shí)間的功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解下2023-12-12vue自定v-model實(shí)現(xiàn)表單數(shù)據(jù)雙向綁定問題
vue.js的一大功能便是實(shí)現(xiàn)數(shù)據(jù)的雙向綁定。這篇文章主要介紹了vue自定v-model實(shí)現(xiàn) 表單數(shù)據(jù)雙向綁定的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09Vue如何用this.$set改變數(shù)組里的某個(gè)值
這篇文章主要介紹了Vue用this.$set改變數(shù)組里的某個(gè)值,文中通過示例代碼介紹了vue中this.$set()的用法----更新數(shù)組和對(duì)象的值,需要的朋友可以參考下2022-12-12詳解Vue如何實(shí)現(xiàn)響應(yīng)式布局
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)響應(yīng)式布局的兩種方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2023-12-12Vue向后臺(tái)傳數(shù)組數(shù)據(jù),springboot接收vue傳的數(shù)組數(shù)據(jù)實(shí)例
這篇文章主要介紹了Vue向后臺(tái)傳數(shù)組數(shù)據(jù),springboot接收vue傳的數(shù)組數(shù)據(jù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11vue3 Teleport瞬間移動(dòng)函數(shù)使用方法詳解
這篇文章主要為大家詳細(xì)介紹了vue3 Teleport瞬間移動(dòng)函數(shù)使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03