vue2.0實(shí)現(xiàn)倒計(jì)時(shí)的插件(時(shí)間戳 刷新 跳轉(zhuǎn) 都不影響)
我發(fā)現(xiàn)好多倒計(jì)時(shí)的插件,刷新都會(huì)變成從頭再來,于是自己用vue2.0寫了一個(gè),測(cè)試通過,直接上代碼
如下是組件代碼:
<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:'已結(jié)束' }, 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}小時(shí)${min}分${sec}秒`; } if(day <= 0 && hour > 0 ){ format = `${hour}小時(shí)${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>
下面是調(diào)用代碼:
<count-down endTime="1490761620" :callback="callback" endText="已經(jīng)結(jié)束了"></count-down>
ednTime 是時(shí)間結(jié)束之后的時(shí)間戳 callback是結(jié)束之后的回調(diào) endText是結(jié)束之后的文字顯示!
以上所述是小編給大家介紹的vue2.0實(shí)現(xiàn)倒計(jì)時(shí)的插件(時(shí)間戳 刷新 跳轉(zhuǎn) 都不影響),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue-router實(shí)現(xiàn)編程式導(dǎo)航的代碼實(shí)例
今天小編就為大家分享一篇關(guān)于vue-router實(shí)現(xiàn)編程式導(dǎo)航的代碼實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01vue實(shí)現(xiàn)橫屏滾動(dòng)公告效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)橫屏滾動(dòng)公告效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04解決Echarts2豎直datazoom滑動(dòng)后顯示數(shù)據(jù)不全的問題
這篇文章主要介紹了解決Echarts2豎直datazoom滑動(dòng)后顯示數(shù)據(jù)不全的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07vue-electron項(xiàng)目創(chuàng)建記錄及問題小結(jié)解決方案
這篇文章主要介紹了vue-electron項(xiàng)目創(chuàng)建記錄及注意事項(xiàng),本文給大家分享了運(yùn)行項(xiàng)目報(bào)錯(cuò)的問題小結(jié)及多種解決方案,需要的朋友可以參考下2024-03-03