// 防抖函数
|
function debounce(func, delay, immediate = false) {
|
let timer = null;
|
return function () {
|
const context = this;
|
const args = arguments;
|
|
const callNow = immediate && !timer;
|
// 如果定时器已经存在,清除它
|
if (timer) {
|
clearTimeout(timer);
|
}
|
|
// 设置一个新的定时器
|
timer = setTimeout(() => {
|
if (!immediate) {
|
func.apply(context, args);
|
}
|
timer = null;
|
}, delay);
|
|
// 配置立即执行
|
if(callNow == true) {
|
func.apply(context, args)
|
}
|
};
|
}
|
|
// 判空函数
|
function isEmpty(value, zeroIsEmpty = false, falseIsEmpty = false){
|
let val = value
|
// 检查是否为null 或者 undefind
|
if(val === null || val === undefined){
|
return true;
|
}
|
//如果字符串全部是由数字构成的,则转化为数字型
|
if(isAllDigits(val) === true){
|
val = Number(val)
|
}
|
|
// 是否是字符串类型
|
if(typeof val === 'string') {
|
return val.trim().length === 0;
|
}
|
|
// 是否是数组
|
if(Array.isArray(val)) {
|
return val.length === 0;
|
}
|
|
//是否是对象
|
if(typeof val === 'object') {
|
return Object.keys(val).length === 0;
|
}
|
|
// 数字类型默认不为空
|
if(typeof val === 'number'){
|
// 数字为0视为空
|
if(zeroIsEmpty == true){
|
if(val === 0){
|
return true
|
}
|
}
|
return false
|
}
|
|
// 布尔类型默认不为空
|
if (typeof val === 'boolean') {
|
// false值视为空
|
if(falseIsEmpty == true){
|
if(val === 0){
|
return true
|
}
|
}
|
return false;
|
}
|
}
|
|
// 判断是否全是数字
|
function isAllDigits(str) {
|
return /^\d+$/.test(str);
|
}
|