|
|
import{D as t,E as e,G as i,H as s,o as a,c as n,w as o,O as m,d as r,e as h,t as c,j as u,i as l}from"./index-DkGewvWo.js";import{_ as d}from"./_plugin-vue_export-helper.BCo6x5W8.js";const T=t({props:{time:{type:[String,Number],default:()=>e.countDown.time},format:{type:String,default:()=>e.countDown.format},autoStart:{type:Boolean,default:()=>e.countDown.autoStart},millisecond:{type:Boolean,default:()=>e.countDown.millisecond}}});function f(t,e=2){let i=`${t}`;for(;i.length<e;)i=`0${i}`;return i}function p(t){return{days:Math.floor(t/864e5),hours:Math.floor(t%864e5/36e5),minutes:Math.floor(t%36e5/6e4),seconds:Math.floor(t%6e4/1e3),milliseconds:Math.floor(t%1e3)}}const g=d({name:"u-count-down",mixins:[i,s,T],data:()=>({timer:null,timeData:p(0),formattedTime:"0",runing:!1,endTime:0,remainTime:0}),watch:{time(t){this.reset()}},mounted(){this.init()},emits:["change","finish"],methods:{init(){this.reset()},start(){this.runing||(this.runing=!0,this.endTime=Date.now()+this.remainTime,this.toTick())},toTick(){this.millisecond?this.microTick():this.macroTick()},macroTick(){this.clearTimeout(),this.timer=setTimeout((()=>{const t=this.getRemainTime();var e,i;e=t,i=this.remainTime,(Math.floor(e/1e3)!==Math.floor(i/1e3)||0===t)&&this.setRemainTime(t),0!==this.remainTime&&this.macroTick()}),30)},microTick(){this.clearTimeout(),this.timer=setTimeout((()=>{this.setRemainTime(this.getRemainTime()),0!==this.remainTime&&this.microTick()}),50)},getRemainTime(){return Math.max(this.endTime-Date.now(),0)},setRemainTime(t){this.remainTime=t;const e=p(t);this.$emit("change",e),this.formattedTime=function(t,e){let{days:i,hours:s,minutes:a,seconds:n,milliseconds:o}=e;return-1===t.indexOf("DD")?s+=24*i:t=t.replace("DD",f(i)),-1===t.indexOf("HH")?a+=60*s:t=t.replace("HH",f(s)),-1===t.indexOf("mm")?n+=60*a:t=t.replace("mm",f(a)),-1===t.indexOf("ss")?o+=1e3*n:t=t.replace("ss",f(n)),t.replace("SSS",f(o,3))}(this.format,e),t<=0&&(this.pause(),this.$emit("finish"))},reset(){this.pause(),this.remainTime=this.time,this.setRemainTime(this.remainTime),this.autoStart&&this.start()},pause(){this.runing=!1,this.clearTimeout()},clearTimeout(){clearTimeout(this.timer),this.timer=null}},beforeUnmount(){this.clearTimeout()}},[["render",function(t,e,i,s,d,T){const f=u,p=l;return a(),n(p,{class:"u-count-down"},{default:o((()=>[m(t.$slots,"default",{},(()=>[r(f,{class:"u-count-down__text"},{default:o((()=>[h(c(d.formattedTime),1)])),_:1})]),!0)])),_:3})}],["__scopeId","data-v-0a1a6ab6"]]);export{g as _};
|