import dayjs from "dayjs"; export const setProperty = (prop: string, val: any, dom = document.documentElement) => { dom.style.setProperty(prop, val); }; export const mix = (color1: string, color2: string, weight: number = 0.5): string => { let color = '#'; for (let i = 0; i <= 2; i++) { const c1 = parseInt(color1.substring(1 + i * 2, 3 + i * 2), 16); const c2 = parseInt(color2.substring(1 + i * 2, 3 + i * 2), 16); const c = Math.round(c1 * weight + c2 * (1 - weight)); color += c.toString(16).padStart(2, '0'); } return color; }; /** * * @param {*} time 时间 * @param {*} fmStr 格式化格式 YYYY-MM-DD HH:mm:ss * @returns */ export function format(time: Date | string, fmStr: string = "YYYY-MM-DD HH:mm:ss") { if (!time) return ""; return dayjs(typeof time == 'string' ? new Date(time) : time).format(fmStr); } /** * * @param fn 需要防抖的函数 * @param time 防抖时间 * @returns */ export const debounce = (fn: Function, time: number = 300) => { let timer; return function (...argu) { if (timer) { clearTimeout(timer); timer = null; } timer = setTimeout(() => { fn(...argu); clearTimeout(timer); timer = null; }, time); }; } /** * * @param fn 需要节流的函数 * @param time 需要节流的时间 * @returns */ export const throttle = (fn: Function, time: number = 300) => { let flag = true return function (...argu) { if (!flag) { return } flag = false let timer = setTimeout(() => { fn(...argu) flag = true clearTimeout(timer) timer = null }, time) } }