更新常用的js工具函数

在手机调试时打印代码
<script src="https://cdn.bootcss.com/vConsole/3.3.0/vconsole.min.js"></script>
<script>
 var vConsole = new VConsole();
 console.log('VConsole is cool');
</script>
import * as date from "./date.js"; /** * 函数防抖 (只执行最后一次点击) * @param fn * @param delay * @returns {Function} * @constructor */ export const Debounce = (fn, t) => { let delay = t || 500; let timer; console.log(fn) console.log(typeof fn) return function () { let args = arguments; if (timer) { clearTimeout(timer); } timer = setTimeout(() => { timer = null; fn.apply(this, args); }, delay); } }; /** * 函数节流 * @param fn * @param interval * @returns {Function} * @constructor */ export const Throttle = (fn, t) => { let last; let timer; let interval = t || 500; return function () { let args = arguments; let now = +new Date(); if (last && now - last < interval) { clearTimeout(timer); timer = setTimeout(() => { last = now; fn.apply(this, args); }, interval); } else { last = now; fn.apply(this, args); } } }; /** * 转换日期格式为2019年11月11日、2019年正月初一 * @param {*} name * @param {*} value * @param {*} expiredays */ export const getChangeDate = (info, type) => { let birthInfoText = '' // 日期互转 const lunar = date.solar2lunar( parseInt(info.birthDayYear), parseInt(info.birthDayMonth), parseInt(info.birthDayDay), ); console.log(info); if (type == 2) { birthInfoText = `农历 ${lunar.lYear||1985}${lunar.IMonthCn||1}${lunar.IDayCn||1}`; } else { birthInfoText = `公历 ${info.birthDayYear||1985}${info.birthDayMonth||1}${info.birthDayDay||1}日`; } return birthInfoText } // 存储cookie export const SetCookie = (name, value, expiredays) => { var exdate = new Date(); exdate.setDate(exdate.getDate() + expiredays); var cookieVal = name + "=" + encodeURI(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())+ ";path=/"; document.cookie = cookieVal; } /*读取cookie*/ export const GetCookie = (sKey) => { sKey = sKey.replace(/([.[]$])/g, '\$1'); var rep = new RegExp(sKey + '=([^;]*)?;', 'i'); var co = document.cookie + ';'; var res = co.match(rep); if (res) { return decodeURI(res[1]) || ""; } else { return null; } }; /*删除cookie*/ export const DelCookie = (sKey) => { SetCookie(sKey,"",-1) } // 获取参数 export const getQueryString = (name) => { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return ""; } // 键盘 export const hackScrollTop = (vnode, offsetVal) => { let target = vnode; let t = navigator.userAgent; // 区分组件和DOM target.offsetTop || (target = vnode.$el); if (t.indexOf("Android") > -1 || t.indexOf("Adr") > -1) { document.documentElement.scrollTop = document.body.scrollTop = target.offsetTop + parseInt(offsetVal); } setTimeout(() => { target.scrollIntoViewIfNeeded(); }, 400); } // 定位 export const stackScroll = (vnode, offsetVal) => { let target = vnode; let t = navigator.userAgent; // 区分组件和DOM target.offsetTop || (target = vnode.$el); document.documentElement.scrollTop = document.body.scrollTop = target.offsetTop + parseInt(offsetVal); setTimeout(() => { target.scrollIntoViewIfNeeded(); }, 400); } // 判断是否是数字 export const isRealNum = (val) => { // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除 if (val === "" || val == null) { return false; } if (!isNaN(val)) { return true; } else { return false; } } // 判断null NaN undefined export const isHasValue = (val) => { if(val){ if (!val) { return false; }else if(val=='undefined'||val=='null'||val==' '||val=='NaN'){ return false; }else{ return true } }else{ return false } } //获取QS
export default function (search) {
  const map = Object.create(null);
  if (search[0] === '?') {
    search = search.substring(1);
  }
  const substrs = search.split('&');
  const len = substrs.length;
  for (let index = 0, substr; index < len; index += 1) {
    substr = substrs[index].split('=');
    if (substr[0]) {
      map[substr[0]] = substr[1];
    }
  }
  return map;
};
 
原文地址:https://www.cnblogs.com/xuniannian/p/10843847.html