uv-ui
Version:
基于vue3的移动端组件库
58 lines (57 loc) • 1.61 kB
JavaScript
import { ref as F, computed as g, onBeforeUnmount as k, onActivated as S, onDeactivated as U } from "vue";
import { inBrowser as q } from "../utils/common.js";
import { useRequestAnimationFrame as x } from "./useRequestAnimationFrame.js";
const { requestAnimationFrame: R, cancelAnimationFrame: B } = x(), s = 1e3, u = 60 * s, f = 60 * u, T = 24 * f;
function E(e) {
const o = Math.floor(e / T), i = Math.floor(e % T / f), t = Math.floor(e % f / u), r = Math.floor(e % u / s), n = Math.floor(e % s);
return {
total: e,
days: o,
hours: i,
minutes: t,
seconds: r,
milliseconds: n
};
}
function I(e, o) {
return Math.floor(e / 1e3) === Math.floor(o / 1e3);
}
function H(e) {
let o, i, t, r;
const n = F(e.time), l = g(() => E(n.value)), c = () => {
t = !1, B(o);
}, m = () => Math.max(i - Date.now(), 0), d = (a) => {
var D, A;
n.value = a, (D = e.onChange) == null || D.call(e, l.value), a === 0 && (c(), (A = e.onFinish) == null || A.call(e));
}, h = () => {
o = R(() => {
t && (d(m()), n.value > 0 && h());
});
}, v = () => {
o = R(() => {
if (t) {
const a = m();
(!I(a, n.value) || a === 0) && d(a), n.value > 0 && v();
}
});
}, M = () => {
!q || (e.millisecond ? h() : v());
}, w = () => {
t || (i = Date.now() + n.value, t = !0, M());
}, C = (a = e.time) => {
c(), n.value = a;
};
return k(c), S(() => {
r && (t = !0, r = !1, M());
}), U(() => {
t && (c(), r = !0);
}), {
start: w,
pause: c,
reset: C,
current: l
};
}
export {
H as useCountDown
};