利用momentJs做一個(gè)倒計(jì)時(shí)組件(實(shí)例代碼)
今天抽空給大家介紹下vue和moment做的一個(gè)倒計(jì)時(shí),具體內(nèi)容如下所示:
展示樣式:

<template>
<div class="table-right flex-a-center">
<div class="time-text">
<span class="timeTextSpan" v-for="item,index of h" >{{item}}</span>
<span class="timeTextSpan1" >: </span>
<span class="timeTextSpan" v-for="item,index of m" >{{item}}</span>
<span class="timeTextSpan1" >: </span>
<span class="timeTextSpan" v-for="item,index of s" >{{item}}</span>
</div>
</div>
</template>
<script>
import moment from 'moment'
export default {
props: {
endTime: { }, //接收得最后時(shí)間 2021-12-17 16:29:20
},
data() {
//這里存放數(shù)據(jù)
return {
h:'00',
m:'00',
s:'00',
timer:null
};
},
watch: {
endTime: {
handler(e) {
if (e) {
let self = this
clearInterval(this.timer)
this.timer = setInterval(function(){self.init()},1000)
}
},
deep: true,
immediate: true
}
},
mounted() {
let self = this
self.init()
clearInterval(this.timer)
this.timer = setInterval(function(){self.init()},1000)
},
//方法集合
methods: {
init(){
let time =moment(this.endTime).diff(moment())
if(time <= 0){
clearInterval(this.timer)
this.onOver()
return
}
let t = time / 1000;
let d = Math.floor(t / (24 * 3600)); //剩余天數(shù),如果需要可以自行補(bǔ)上
let h = Math.floor((t - 24 * 3600 * d) / 3600) + d*24; //不需要天數(shù),把天數(shù)轉(zhuǎn)換成小時(shí)
let _h = Math.floor((t - 24 * 3600 * d) / 3600) //保留天數(shù)后得小時(shí)
let m = Math.floor((t - 24 * 3600 * d - _h * 3600) / 60);
let s = Math.floor((t - 24 * 3600 * d - _h * 3600 - m * 60));
this.h = String(h).length == 1? '0'+String(h):String(h)
this.m = String(m).length == 1? '0'+String(m):String(m)
this.s = String(s).length == 1? '0'+String(s):String(s)
},
onOver() {
this.$emit('over') //倒計(jì)時(shí)結(jié)束得回調(diào)
}
},
beforeDestroy(){
this.timer = null
clearInterval(this.timer)
}
}
</script>
<style lang='less' scoped>
@import url("@/assets/css/supplier.less");
.table-right {
font-size: 12px;
color: #757e8a;
.timeTextSpan{
display: inline-block;
width: 14px;
height: 22px;
text-align: center;
background: #F1F0F0;
border-radius: 2px;
margin-right: 2px;
font-size: 16px;
color: #ff8a2b;
font-weight: bold;
}
.timeTextSpan1{
display: inline-block;
width: 14px;
text-align: center;
vertical-align: bottom;
color:#202D40;
font-size: 16px;
font-weight: bold;
}
.time-text {
margin-left: 10px;
}
}
</style>
到此這篇關(guān)于利用momentJs做一個(gè)倒計(jì)時(shí)組件的文章就介紹到這了,更多相關(guān)momentJs倒計(jì)時(shí)組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript input輸入框模糊提示功能的實(shí)現(xiàn)
這篇文章主要介紹了javascript input輸入框模糊提示功能的實(shí)現(xiàn)的相關(guān)資料,希望通過本能幫助到大家,需要的朋友可以參考下2017-09-09
Javascript查詢DBpedia小應(yīng)用實(shí)例學(xué)習(xí)
本文則嘗試?yán)肧PARQLWrapper.js來(lái)讀取DBpedia的數(shù)據(jù),并顯示出來(lái),感興趣的你可以參考下,或許對(duì)你有所幫助2013-03-03
PHP實(shí)現(xiàn)本地圖片上傳和驗(yàn)證功能
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)本地圖片上傳和驗(yàn)證功能的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
JS從數(shù)組中隨機(jī)取出幾個(gè)數(shù)組元素的方法
JS如何從一個(gè)數(shù)組中隨機(jī)取出一個(gè)元素或者幾個(gè)元素呢?其實(shí)方法很簡(jiǎn)單,下面小編給大家分享了JS隨機(jī)取出幾個(gè)數(shù)組元素的方法,非常不錯(cuò),需要的朋友參考下2016-08-08
JavaScript中常用的字符串方法函數(shù)操作方法總結(jié)
這篇文章主要介紹了JavaScript中所有的字符串函數(shù)操作方法整理匯總,包括字符串的長(zhǎng)度、連接、查找、截取、替換、分隔、轉(zhuǎn)換等處理方法,以及網(wǎng)址中獲取文件名等等,需要的朋友可以參考下2023-12-12
一文帶你搞懂JavaScript中轉(zhuǎn)義字符的使用
說(shuō)起轉(zhuǎn)義字符,大家最先想到的肯定是使用反斜杠,這也是我們最常見的,很多編程語(yǔ)言都支持。除了反斜杠以外,在前端開發(fā)中,還有其他幾種轉(zhuǎn)義字符,也是較常見的,本文將對(duì)這些做一個(gè)總結(jié)2023-02-02
使用IE的地址欄來(lái)輔助調(diào)試Web頁(yè)腳本
使用IE的地址欄來(lái)輔助調(diào)試Web頁(yè)腳本...2007-03-03
autojs 螞蟻森林能量自動(dòng)拾取即給指定好友澆水的實(shí)現(xiàn)方法
這篇文章主要介紹了autojs 螞蟻森林能量自動(dòng)拾取即給指定好友澆水的實(shí)現(xiàn)方法,本文通過圖文并茂實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05

