function subTimeStr(str) { if (!str || typeof str != "string") return; let index = str.indexOf(" "); return str.substring(0, index); } function datetimeFormat(type = "yyyy-MM-dd HH:mm:ss", t = new Date()) { let timer, W; let _d = new Date(t); let yyyy = _d.getFullYear(); let M, MM = _d.getMonth() + 1; if (MM < 10) MM = "0" + MM; let d, dd = _d.getDate(); if (dd < 10) dd = "0" + dd; let h, hh, H, HH = _d.getHours(); if (h > 12) h = h - 12; if (hh > 12) hh = hh - 12; if (hh < 10) hh = "0" + hh; if (HH < 10) HH = "0" + HH; let mm = _d.getMinutes(); if (mm < 10) mm = "0" + mm; let ss = _d.getSeconds(); if (ss < 10) ss = "0" + ss; let w = _d.getDay(); if (0 == w) w = 7; switch (type) { case "yyyy-MM-dd HH:mm:ss": { timer = `${yyyy}-${MM}-${dd} ${HH}:${mm}:${ss}`; break; } case "MM-dd HH:mm:ss": { timer = `${MM}-${dd} ${HH}:${mm}:${ss}`; break; } case "yyyy-MM-dd": { timer = `${yyyy}-${MM}-${dd}`; break; } case "HH:mm:ss": { timer = `${HH}:${mm}:${ss}`; break; } case "yyyy/MM/dd": { timer = `${yyyy}/${MM}/${dd}`; break; } case "年月日": { timer = `${yyyy}年${M}月${d}日`; break; } case "年月月日日": { timer = `${yyyy}年${MM}月${dd}日`; break; } } return timer; } const throttle = (fn, wait) => { //节流 var prev = Date.now(); return function () { var context = this; var args = arguments; var now = Date.now(); console.log(now - prev > wait) if (now - prev > wait) { fn.apply(context, args) prev = Date.now() } } } const debounce = (func, wait) => { //防抖 // wait:500ms;func:被频繁触发的事件 let timeout; return function () { let context = this; let args = arguments; let later = () => { timeout = null; func.apply(context, args); }; clearTimeout(timeout); timeout = setTimeout(later, wait); } } module.exports = { subTimeStr, datetimeFormat, throttle, debounce }