2a145a18 by simon

1、路由兼容name写法

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