這篇文章將為大家詳細(xì)講解有關(guān)vue如何實(shí)現(xiàn)計(jì)時(shí)器組件,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

實(shí)例如下:
<template>
<div>
<span
:sendSync="sendSync"
:autoStart="autoStart"
:defaultVal="defaultVal"
>{{countString}}</span>
</div>
</template>
<script>
export default {
data() {
return {
isStart: false,
globalTimer: null,//獲取setInterval對(duì)象值
countString: '0秒', //用來顯示時(shí)間
day: 0,
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
countVal: this.defaultVal, //獲取初始值
pauseTime: 0,
}
},
watch: {
'countString': {
deep: true,
handler: function(val, oldVal) {
var vm = this
if (vm.needSendSunc) {
vm.passToParent(val)
}
}
},
'needSendSunc': {
deep: true,
handler: function(val) {
var vm = this
if (val) {
vm.passToParent(vm.countString)
}
}
}
},
props: {
sendSync: {
type: Boolean,
default: false,
},
autoStart: {
type: Boolean,
default: false,
},
defaultVal: {
type: Number,
default: 0,
}
},
mounted() {
var vm = this
if (vm.autoStart) {
vm.startCountFn()
}
},
computed: {
needSendSunc: function() {
return this.sendSync
}
},
created: function() {
this.$on('startCount', function() {
this.startCountFn()
});
this.$on('stopCount', function() {
this.stopCountFn()
});
},
components: {},
methods: {
counterFn: function(counterTime) {
var vm = this
var nowDate = new Date().getTime()
if(vm.pauseTime == 0){
var num = nowDate - counterTime //計(jì)算時(shí)間毫秒差
}else{
vm.pauseTime = vm.pauseTime +10
var num = vm.pauseTime - counterTime //計(jì)算時(shí)間毫秒差
}
var leave1 = num % (24 * 3600 * 1000) //計(jì)算天數(shù)后剩余的毫秒數(shù)
var leave2 = leave1 % (3600 * 1000) //計(jì)算小時(shí)數(shù)后剩余的毫秒數(shù)
var leave3 = leave2 % (60 * 1000) //計(jì)算分鐘數(shù)后剩余的毫秒數(shù)
vm.day = Math.floor(num / (24 * 3600 * 1000)) //計(jì)算相差天數(shù)
vm.hour = Math.floor(leave1 / (3600 * 1000))//計(jì)算相差小時(shí)
vm.minute = Math.floor(leave2 / (60 * 1000))//計(jì)算相差分鐘
vm.second = Math.round(leave3 / 1000) //計(jì)算相差秒
if (vm.day > 0) {
vm.countString = `${vm.day}天 ${vm.hour}小時(shí) ${vm.minute}分 ${vm.second}秒`;
} else if (vm.hour > 0) {
vm.countString = `${vm.hour}小時(shí) ${vm.minute}分 ${vm.second}秒`;
} else if (vm.minute > 0) {
vm.countString = `${vm.minute}分 ${vm.second}秒`;
} else {
vm.countString = `${vm.second}秒`;
}
},
startCountFn: function() {
var vm = this
if (!vm.isStart) {
vm.countVal = vm.countVal ? vm.countVal : new Date().getTime()
var timer = setInterval(function() {
vm.counterFn(vm.countVal)
}, 10)
vm.globalTimer = timer
vm.isStart = true
}
},
stopCountFn: function() {
var vm = this
if (vm.isStart) {
window.clearInterval(vm.globalTimer)
vm.globalTimer = null;
vm.isStart = false
vm.pauseTime = new Date().getTime()
}
},
passToParent: function(data) {
var vm = this
this.$emit("getDataFromChild", data)
},
}
}
</script>
<style>
</style>關(guān)于“vue如何實(shí)現(xiàn)計(jì)時(shí)器組件”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前標(biāo)題:vue如何實(shí)現(xiàn)計(jì)時(shí)器組件-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://chinadenli.net/article6/cosoig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、商城網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、營(yíng)銷型網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容