/** * 创建一个节流函数,用于限制函数的执行频率。 * 节流函数确保在指定的时间间隔内,即使被多次调用,也只会执行一次。 * * @param func 被包装的函数,当达到指定时间间隔时将被调用。 * @param interval 时间间隔(以毫秒为单位),在此期间内func最多执行一次。 * @returns 返回一个新的函数,该函数会在调用频率超过interval时执行func。 * * 注意:该函数使用了时间戳来判断是否达到执行间隔,以确保func不会在interval时间内被重复执行。 */ export function throttle(func: (...args: any[]) => void, interval: number) { // 上次执行func的时间戳 let lastTime = 0; // 返回一个新的函数,该函数会根据时间间隔限制func的执行频率 return (...args: any[]) => { // 当前时间戳 const now = Date.now(); // 检查是否达到执行间隔 if (now - lastTime >= interval) { // 执行被包装的函数 func(...args); // 更新上次执行的时间戳 lastTime = now; } }; } export const getTargetDomByPointerEvent = ( e: PointerEvent ): HTMLElement | null => { if (!e || !e.target) { return null; } try { const target = e.target as HTMLElement; if (!(target instanceof HTMLElement)) { return null; } // 首先尝试使用 e.target if (target instanceof HTMLElement) { return target; } // 如果需要更高的精度,使用 document.elementFromPoint const el = document.elementFromPoint(e.pageX, e.pageY); return el instanceof HTMLElement ? el : null; } catch (error) { console.error("Error in getTargetDomByPointerEvent:", error); return null; } }; // 传入一个元素,和一个选择器。判断当前元素或者其父元素中是否存在符合选择器的元素 // 找到存在的父元素,返回该元素 export const findParentBySelector = ( el: HTMLElement, selector?: string ): HTMLElement | null => { // 输入验证 if (!el || !selector) { return null; } let currentEl: HTMLElement | null = el; // 使用兼容性更好的 matches 方法 const matches = (element: HTMLElement, selector: string) => { const matchesSelector = element.matches || element.webkitMatchesSelector || (element as any)?.msMatchesSelector; return matchesSelector.call(element, selector); }; while (currentEl) { if (matches(currentEl, selector)) { return currentEl; } currentEl = currentEl.parentElement; } return null; }; // 判断当前协议是否是http或https协议 export const isHttpProtocol = () => { return ( window.location.protocol === "http:" || window.location.protocol === "https:" ); }; // 获取当前年-月-日的函数 export const getCurrentDate = () => { const date = new Date(); const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); return `${year}-${month}-${day}`; }; // 辅助函数:从日期对象中提取年、月、日信息并创建新的日期对象 const getDateWithoutTime = (date: Date): Date => { const year = date.getFullYear(); const month = date.getMonth(); const day = date.getDate(); return new Date(year, month, day); }; // 比较当前日期和给定时间戳对应的日期,判断当前日期是否大于给定时间戳对应的日期 export function compareDatesByTimestamp( timestampToCheck: string | number ): boolean { // 检查输入的时间戳是否有效 if ( typeof timestampToCheck !== "string" && typeof timestampToCheck !== "number" ) { console.error( "Invalid input: timestampToCheck must be a string or a number." ); return false; } try { const currentTimestamp = Date.now(); const dateToCheck = new Date(timestampToCheck); const currentDate = new Date(currentTimestamp); // 检查日期对象是否有效 if (isNaN(dateToCheck.getTime())) { console.error( "Invalid timestamp: cannot parse timestampToCheck to a valid date." ); return false; } // 使用辅助函数获取只包含年、月、日的日期对象 const checkDate = getDateWithoutTime(dateToCheck); const curDate = getDateWithoutTime(currentDate); // 判断当前日期是否大于要检查的日期 const isGreater = curDate > checkDate; return isGreater; } catch (error) { console.error("Error in compareDatesByTimestamp:", error); return false; } }