| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- 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
- }
|