84 lines
2.1 KiB
JavaScript
84 lines
2.1 KiB
JavaScript
// -防抖
|
|
export function debounce(fn, wait) {
|
|
let delay = wait || 500
|
|
let timer
|
|
return function () {
|
|
let args = arguments;
|
|
if (timer) {
|
|
clearTimeout(timer)
|
|
console.log('拦截')
|
|
}
|
|
let callNow = !timer
|
|
timer = setTimeout(() => {
|
|
console.log('发送')
|
|
timer = null
|
|
}, delay)
|
|
if (callNow) fn.apply(this, args)
|
|
}
|
|
}
|
|
|
|
// -节流
|
|
export function throttle(fn, wait) {
|
|
let delay = wait || 500
|
|
let timer = null
|
|
return function () {
|
|
if (timer) {
|
|
console.log('拦截');
|
|
return
|
|
}
|
|
timer = setTimeout(() => {
|
|
console.log('发送');
|
|
fn.apply(this, arguments)
|
|
timer = null
|
|
}, delay)
|
|
}
|
|
}
|
|
|
|
// 截取参数
|
|
export default function getPageParams() {
|
|
//取得查询字符串并去掉开头的问号
|
|
var qs = location.href.split('?')[1] || '',
|
|
//保存数据的对象
|
|
args = {},
|
|
//取得每一项
|
|
items = qs.length ? qs.split("&") : [],
|
|
item = null,
|
|
name = null,
|
|
value = null,
|
|
//在for 循环中使用
|
|
i = 0,
|
|
len = items.length;
|
|
console.log('qs', qs)
|
|
//逐个将每一项添加到args 对象中
|
|
for (i = 0; i < len; i++) {
|
|
item = items[i].split("=");
|
|
name = decodeURIComponent(item[0]);
|
|
value = decodeURIComponent(item[1]);
|
|
if (name.length) {
|
|
args[name] = value;
|
|
}
|
|
}
|
|
return args;
|
|
}
|
|
|
|
//格式化日期
|
|
export function dateFormat(format, date) {
|
|
if (!format) return '';
|
|
date = date || new Date();
|
|
let dateMap = {
|
|
y: date.getFullYear(),
|
|
M: date.getMonth() + 1,
|
|
d: date.getDate(),
|
|
h: date.getHours(),
|
|
m: date.getMinutes(),
|
|
s: date.getSeconds(),
|
|
S: date.getMilliseconds()
|
|
};
|
|
return format.replace(/(y+)|(M+)|(d+)|(h+)|(m+)|(s+)|(S+)/g, (a) => _add0(dateMap[a[0]], a.length))
|
|
}
|
|
|
|
function _add0(time, len) {
|
|
time = time.toString();
|
|
let l = time.length;
|
|
return l < len ? '0'.repeat(len - l) + time : time;
|
|
} |