目录
7、判断两个 Oject 是否相等 isEqualObject
8、判断 Object 是否为空 isEmptyObject
10、获取小程序链接的地址栏参数 GetWxMiniProgramUrlParam
- function isNumber(val){
- if(val==""){
- return false;
- }
- if(val.length>1){
- if(val.substr(val.length-1, val.length)=="."){
- var len = val.split(".").length-1;
- if(len>1){
- return false;
- }
- val = val.substr(0, val.length-1);
- }
- }
-
- var regPos = /^\d+(\.\d+)?$/;
- var regNeg = /^((([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/;
- if(regPos.test(val) || regNeg.test(val)) {
- return true;
- }
- return false;
- }
- //计算字符长度(一个汉字2个字符)
- function calculateStrLengh(str){
- var len=0;
- if(!isExist(str)){
- return 0;
- }
- for (var i=0; i
length; i++) { - var c = str.charCodeAt(i);
- //单字节加1
- if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
- len++;
- }
- else {
- len+=2;
- }
- }
- return len;
- }
- //转换日期格式(时间戳转换为datetime格式)
- function changeDateFormat(cellval, hms) {
- var dateVal = cellval + "";
- if (cellval != null) {
- var date = new Date(parseInt(dateVal.replace("/Date(", "").replace(")/", ""), 10));
- var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
- var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
-
- var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
- var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
- var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
- if(hms){
- return date.getFullYear() + "-" + month + "-" + currentDate + " " + hours + ":" + minutes + ":" + seconds;
- }else{
- return date.getFullYear() + "-" + month + "-" + currentDate;
- }
- }
- }
- /**
- * 节流
- */
- function throttle(fn, wait = 500, isImmediate = false) {
- let flag = true;
- if (isImmediate) {
- return function() {
- if (flag) {
- fn.apply(this, arguments);
- flag = false;
- setTimeout(() => {
- flag = true
- }, wait)
- }
- }
- }
- return function() {
- if (flag == true) {
- flag = false
- setTimeout(() => {
- fn.apply(this, arguments)
- flag = true
- }, wait)
- }
- }
- }
- //防抖
- function debounce(fn, wait, isImmediate) {
- let timerId = null;
- let flag = true;
- if (isImmediate) {
- return function() {
- clearTimeout(timerId);
- if (flag) {
- this[fn]() ;
- flag = false
- }
- timerId = setTimeout(() => {
- flag = true
- }, wait)
- }
- }
- return function() {
- clearTimeout(timerId);
- timerId = setTimeout(() => {
- this[fn]()
- }, wait)
- }
- }
- //js获取地址栏参数 name为参数名
- function GetUrlParam (name) {
- var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
- var r = window.location.search.substr(1).match(reg);
- if (r != null) {
- return unescape(r[2]);
- }
- return null;
- }
- // 判断两个object是否相等
- function isEqualObject (obj1, obj2) {
- var o1 = obj1 instanceof Object;
- var o2 = obj2 instanceof Object;
- if (!o1 || !o2) { /* 判断不是对象 */
- return obj1 === obj2;
- }
-
- if (Object.keys(obj1).length !== Object.keys(obj2).length) {
- return false;
- //Object.keys() 返回一个由对象的自身可枚举属性(key值)组成的数组,例如:数组返回下表:let arr = ["a", "b", "c"];console.log(Object.keys(arr))->0,1,2;
- }
-
- for (var attr in obj1) {
- var t1 = obj1[attr] instanceof Object;
- var t2 = obj2[attr] instanceof Object;
- if (t1 && t2) {
- return diff(obj1[attr], obj2[attr]);
- } else if (obj1[attr] !== obj2[attr]) {
- return false;
- }
- }
- return true;
- }
- //判断object为空
- function isEmptyObject (e) {
- for (var t in e)
- return !1;
- return !0
- }
- function isExist (str) {
- if (str !== null && str !== "undefined" && str !== "" && typeof str != "undefined" && !(typeof (str) == "string" &&
- str
- .split(" ").join("").length == 0)) {
- return true;
- } else {
- return false;
- }
- }
- // 判断是否为IE8
- var DEFAULT_VERSION = 8.0;
- var ua = navigator.userAgent.toLowerCase();
- var isIE = ua.indexOf("msie") > -1;
- var safariVersion;
- if (isIE) {
- safariVersion = ua.match(/msie ([\d.]+)/)[1];
- }