Vue實(shí)現(xiàn)實(shí)時(shí)刷新時(shí)間的功能
前言
實(shí)現(xiàn)大屏的實(shí)時(shí)刷新時(shí)間的功能:在大屏中有時(shí)需要實(shí)現(xiàn)顯示日期和當(dāng)前的時(shí)間的功能,且秒數(shù)需要實(shí)時(shí)刷新。
實(shí)現(xiàn)方法
要點(diǎn):每隔一秒刷新一次。
<div class="date">
{{ time.date }} {{ time.week }} {{ time.time }}
</div>
<script setup>
import { getTime } from '@/utils';
//獲取并刷新日期
const time = ref('');
const getDataTime = () => {
time.value = getTime();
setTimeout(getDataTime, 1000);
};
onMounted(() => {
getDataTime();
});
</script>
//getTime方法
export function getTime() {
const nowDate = new Date()
const date = nowDate.getFullYear() + '-' + _formatNum(nowDate.getMonth() + 1) + '-' + _formatNum(nowDate.getDate())
const time = _formatNum(nowDate.getHours()) + ':' + _formatNum(nowDate.getMinutes()) + ':' + _formatNum(nowDate.getSeconds())
let week = ''
switch (nowDate.getDay()) {
case 0:
week = '星期天'
break
case 1:
week = '星期一'
break
case 2:
week = '星期二'
break
case 3:
week = '星期三'
break
case 4:
week = '星期四'
break
case 5:
week = '星期五'
break
case 6:
week = '星期六'
break
default:
break
}
return {
date,
time,
week
}
}
方法補(bǔ)充
vue獲取當(dāng)前時(shí)間并實(shí)時(shí)刷新
1、在data中聲明變量
data() {
return {
nowDate: null, //存放年月日變量
nowTime: null, //存放時(shí)分秒變量
timer: "", //定義一個(gè)定時(shí)器的變量
currentTime: new Date(), // 獲取當(dāng)前時(shí)間
}
}
2、定義獲取時(shí)間的方法getTime,并在created()聲明周期里面調(diào)用,在實(shí)例創(chuàng)建前調(diào)用
created()
{
this.timer = setInterval(this.getTime, 1000);
}
3、具體方法如下:
methods: {
getTime(){
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour= date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
const str = ''
if(this.hour>12) {
this.hour -= 12;
this.str = " PM";
}else{
this.str = " AM";
}
this.month=check(month);
this.day=check(day);
this.hour=check(hour);
this.minute=check(minute);
this.second=check(second);
function check(i){
const num = (i<10)?("0"+i) : i;
return num;
}
this.nowDate = year + "年" + this.month + "月" + this.day+"日";
this.nowTime = this.hour + ":" + this.minute + ":" + this.second + this.str;
},
}4、離開頁面使用beforeDestroy() 銷毀
beforeDestroy() {
if (this.timer) {
clearInterval(this.timer); // 在Vue實(shí)例銷毀前,清除定時(shí)器
}
}
5、在頁面需要顯示的地方綁定{{ nowDate }},{{ nowTime }}即可
到此這篇關(guān)于Vue實(shí)現(xiàn)實(shí)時(shí)刷新時(shí)間的功能的文章就介紹到這了,更多相關(guān)Vue實(shí)時(shí)刷新時(shí)間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于axios配置請求頭content-type實(shí)例詳解
現(xiàn)在前端開發(fā)中需要通過Ajax發(fā)送請求獲取后端數(shù)據(jù)是很普遍的一件事情了,下面這篇文章主要介紹了關(guān)于axios配置請求頭content-type的相關(guān)資料,需要的朋友可以參考下2022-04-04
vue增加強(qiáng)緩存和版本號的實(shí)現(xiàn)方法
這篇文章主要介紹了vue增加強(qiáng)緩存和版本號的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
聊聊Vue中provide/inject的應(yīng)用詳解
這篇文章主要介紹了聊聊Vue中provide/inject的應(yīng)用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
你了解vue3.0響應(yīng)式數(shù)據(jù)怎么實(shí)現(xiàn)嗎
這篇文章主要介紹了你了解vue3.0響應(yīng)式數(shù)據(jù)怎么實(shí)現(xiàn)嗎,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06
詳解如何解決vue開發(fā)請求數(shù)據(jù)跨域的問題(基于瀏覽器的配置解決)
這篇文章主要介紹了詳解如何解決vue開發(fā)請求數(shù)據(jù)跨域的問題(基于瀏覽器的配置解決),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11

