-
格式化时间戳的函数
// 格式化时间戳的函数 export const formatDateTime = (timestamp, format = 'YYYY-MM-DD hh:mm:ss') => { if (typeof timestamp !== 'number' && typeof timestamp !== 'undefined' && typeof timestamp !== 'string') return Error('timestamp is not a number') if (typeof timestamp === "string") { format = timestamp; timestamp = undefined; } const date = typeof timestamp === 'undefined' ? new Date() : new Date(timestamp) const oReg = { "(Y|y)+": date.getFullYear(), "M+": date.getMonth() + 1, // 月份 "(D|d)+": date.getDate(), // 日 "(H|h)+": date.getHours(), // 小时 "m+": date.getMinutes(), // 分 "s+": date.getSeconds(), // 秒 q: Math.floor((date.getMonth() + 3) / 3), // 季度 Q: ["一", "二", "三", "四"][Math.floor((date.getMonth() + 3) / 3) - 1], // 季度 w: date.getDay(),// 星期 W: ["日", "一", "二", "三", "四", "五", "六"][date.getDay()], // 星期 S: date.getMilliseconds() // 毫秒 }; for (const k in oReg) { const regex = new RegExp(k); if (regex.test(format)) { format = format.replace(regex, format.match(regex)[0].length === 1 ? oReg[k] : ('00' + oReg[k]).slice(-format.match(regex)[0].length)); } } return format; };
-- --
-
JS睡眠sleep(毫秒)
// JS睡眠sleep(毫秒) export const sleep = (numberMillis) => { let exitTime = +new Date() + numberMillis; while (true) { if (+new Date() > exitTime) return; } } // JS睡眠sleep(毫秒) export const sleepSync = (time = 2000) => { return new Promise((resolve) => { setTimeout(() => { resolve(true); }, time); }); }
-- --
-
轮询接口函数 新版
// 轮询接口函数 新版 /** * * @param params recursionAPI * requestFunction 请求函数 * data 请求参数 * success 请求函数执行成功 * e 请求结果 * abort 终止函数,可以结束轮询 * count 当前调用次数 * fail 请求函数执行失败 【暂未实现】 * complete 失败获成功都执行 【暂未实现】 * * callbackFunction 回调函数 * e 请求结果 * maximum 最大调用次数,默认:五次 * delay 每次接口完成后延迟调用时长,默认值:2000,单位:毫秒 */ const recursionCallback = async (params: recursionAPI) => { let that = this; const recursion: Function = async (params: recursionAPI, count = 0, result = {}) => { const abort = () => count = maximum; let { requestFunction, callbackFunction, data = {}, success, fail, complete, maximum = 5, delay = 2000, loadingText = `查询中...`, showLoading = true }: any = params; if (count == maximum) { that.$hideLoading(); callbackFunction(result); return; } if (count) that.sleep(delay); count++; if (showLoading) that.$showLoading(loadingText); result = await requestFunction(data); success(result, abort, count); return await recursion(params, count, result); } return await recursion(params); }
-- -- -
json排序
// json排序 export const paramsSort = (params, res = {}) => { let keys = Object.keys(params).filter(item => params[item] && typeof params[item] === "object"); res = firstParams(params); keys.map(item => { res[item] = paramsSort(params[item], res); }); return res; function firstParams(params) { if (Array.isArray(params)) { // return params.sort(); return params; } else { let keys = Object.keys(params).sort(); let res = {}; keys.map(item => (res[item] = params[item])); return res; } } };
-- -- -
参数转字符串
// 参数转字符串 export const parseParams = (params, type = "string") => { return { init() { switch (type) { case "string": let key = Object.keys(params); let strs = []; for (let k of key) { strs.push(`${k}=${params[k] || ""}
常用的JS函数
于 2024-08-16 10:56:24 首次发布