2a145a18 by simon

1、路由兼容name写法

2、utils修复防抖/节流函数
1 parent 4d62f5c3
......@@ -199,64 +199,71 @@ export function formatDate(date, fmt) {
/**
* @desc 函数防抖
* @param func 函数
* @param wait 延迟执行毫秒数
* @param immediate true 表立即执行,false 表非立即执行
*/
export function debounce(func, wait, immediate) {
let timeout;
return function () {
let context = this;
let args = arguments;
if (timeout) clearTimeout(timeout);
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(() => {
timeout = null;
}, wait)
if (callNow) func.apply(context, args)
} else {
timeout = setTimeout(function () {
func.apply(context, args)
}, wait);
}
}
}
/**
* @desc 函数节流
* @desc 函数防抖
* @param func 函数
* @param wait 延迟执行毫秒数
* @param type 1 表时间戳版,2 表定时器版
* 时间戳版的函数触发是在时间段内开始的时候,而定时器版的函数触发是在时间段内结束的时候。
* @param immediate true 表立即执行,false 表非立即执行
*/
export function throttle(func, wait, type) {
if (type === 1) {
var previous = 0;
} else if (type === 2) {
var timeout;
}
return function () {
let context = this;
let args = arguments;
if (type === 1) {
let now = Date.now();
let debounceTimeout;
if (now - previous > wait) {
func.apply(context, args);
previous = now;
}
} else if (type === 2) {
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args)
}, wait)
}
}
}
}
\ No newline at end of file
export function debounce(func, wait, immediate) {
return function () {
let context = this;
let args = arguments;
if (debounceTimeout) clearTimeout(debounceTimeout);
if (immediate) {
var callNow = !debounceTimeout;
debounceTimeout = setTimeout(() => {
debounceTimeout = null;
}, wait)
if (callNow) func.apply(context, args)
} else {
debounceTimeout = setTimeout(function () {
func.apply(context, args)
}, wait);
}
}();
}
/**
* @desc 函数节流
* @param func 函数
* @param wait 延迟执行毫秒数
* @param type 1 表时间戳版,2 表定时器版
* 时间戳版的函数触发是在时间段内开始的时候,而定时器版的函数触发是在时间段内结束的时候。
*/
let throttleTimeout;
let throttlePrevious;
export function throttle(func, wait, type) {
if (type === 1) {
throttlePrevious = 0;
} else if (type === 2) {
throttleTimeout = 0;
}
return function () {
let context = this;
let args = arguments;
if (type === 1) {
let now = Date.now();
if (now - throttlePrevious > wait) {
func.apply(context, args);
throttlePrevious = now;
}
} else if (type === 2) {
if (!throttleTimeout) {
throttleTimeout = setTimeout(() => {
throttleTimeout = null;
func.apply(context, args)
}, wait)
}
}
}();
}
\ No newline at end of file
......