vue中倒計時組件的實例代碼
更新時間:2018年07月06日 16:01:31 作者:Null_Bugs
這篇文章主要介紹了vue中倒計時組件的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
子組件:
<template> <span :endTime="endTime" :callback="callback" :endText="endText"> <slot> {{content}} </slot> </span> </template> <script> export default { data(){ return { content: '', } }, props:{ endTime:{ type: String, default :'' }, endText:{ type : String, default:'已結束' }, callback : { type : Function, default :'' } }, mounted () { this.countdowm(this.endTime) }, methods: { countdowm(timestamp){ let self = this; let timer = setInterval(function(){ let nowTime = new Date(); let endTime = new Date(timestamp * 1000); let t = endTime.getTime() - nowTime.getTime(); if(t>0){ let day = Math.floor(t/86400000); let hour=Math.floor((t/3600000)%24); let min=Math.floor((t/60000)%60); let sec=Math.floor((t/1000)%60); hour = hour < 10 ? "0" + hour : hour; min = min < 10 ? "0" + min : min; sec = sec < 10 ? "0" + sec : sec; let format = ''; if(day > 0){ format = `${day}天${hour}小時${min}分${sec}秒`; } if(day <= 0 && hour > 0 ){ format = `${hour}小時${min}分${sec}秒`; } if(day <= 0 && hour <= 0){ format =`${min}分${sec}秒`; } self.content = format; }else{ clearInterval(timer); self.content = self.endText; self._callback(); } },1000); }, _callback(){ if(this.callback && this.callback instanceof Function){ this.callback(...this); } } } } </script>
父組件:
<count-down endTime="1590761620" :callback="callback" endText="已經(jīng)結束了"></count-down> methods:{ callback:function(){ } }
總結
以上所述是小編給大家介紹的vue中倒計時組件的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Spring boot 和Vue開發(fā)中CORS跨域問題解決
這篇文章主要介紹了Spring boot 和Vue開發(fā)中CORS跨域問題解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09Vue.js中class與style的增強綁定實現(xiàn)方法
由于Class和Style綁定使用頻繁,字符串拼接麻煩且易錯,因此,Vue.js 做了專門的增強,表達式結果的類型除了字符串之外,還可以是對象或數(shù)組,本文給大家講解Vue.js中class與style的增強綁定知識,感興趣的朋友一起看看吧2023-04-04實例詳解Vue項目使用eslint + prettier規(guī)范代碼風格
這篇文章主要介紹了Vue項目使用eslint + prettier規(guī)范代碼風格,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2018-08-08基于Vue2實現(xiàn)移動端圖片上傳、壓縮、拖拽排序、拖拽刪除功能
這篇文章主要介紹了基于Vue2實現(xiàn)移動端圖片上傳、壓縮、拖拽排序、拖拽刪除功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01淺談vue中數(shù)據(jù)雙向綁定的實現(xiàn)原理
本篇文章主要介紹了淺談vue中數(shù)據(jù)雙向綁定的實現(xiàn)原理 ,主要使用v-model這個數(shù)據(jù)雙向綁定,有興趣的可以了解一下2017-09-09