1、路由兼容name写法
2、utils修复防抖/节流函数
Showing
1 changed file
with
60 additions
and
53 deletions
| ... | @@ -199,64 +199,71 @@ export function formatDate(date, fmt) { | ... | @@ -199,64 +199,71 @@ export function formatDate(date, fmt) { |
| 199 | 199 | ||
| 200 | 200 | ||
| 201 | 201 | ||
| 202 | /** | ||
| 203 | * @desc 函数防抖 | ||
| 204 | * @param func 函数 | ||
| 205 | * @param wait 延迟执行毫秒数 | ||
| 206 | * @param immediate true 表立即执行,false 表非立即执行 | ||
| 207 | */ | ||
| 208 | export function debounce(func, wait, immediate) { | ||
| 209 | let timeout; | ||
| 210 | 202 | ||
| 211 | return function () { | ||
| 212 | let context = this; | ||
| 213 | let args = arguments; | ||
| 214 | 203 | ||
| 215 | if (timeout) clearTimeout(timeout); | ||
| 216 | if (immediate) { | ||
| 217 | var callNow = !timeout; | ||
| 218 | timeout = setTimeout(() => { | ||
| 219 | timeout = null; | ||
| 220 | }, wait) | ||
| 221 | if (callNow) func.apply(context, args) | ||
| 222 | } else { | ||
| 223 | timeout = setTimeout(function () { | ||
| 224 | func.apply(context, args) | ||
| 225 | }, wait); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | } | ||
| 229 | 204 | ||
| 230 | /** | 205 | /** |
| 231 | * @desc 函数节流 | 206 | * @desc 函数防抖 |
| 232 | * @param func 函数 | 207 | * @param func 函数 |
| 233 | * @param wait 延迟执行毫秒数 | 208 | * @param wait 延迟执行毫秒数 |
| 234 | * @param type 1 表时间戳版,2 表定时器版 | 209 | * @param immediate true 表立即执行,false 表非立即执行 |
| 235 | * 时间戳版的函数触发是在时间段内开始的时候,而定时器版的函数触发是在时间段内结束的时候。 | ||
| 236 | */ | 210 | */ |
| 237 | export function throttle(func, wait, type) { | 211 | let debounceTimeout; |
| 238 | if (type === 1) { | ||
| 239 | var previous = 0; | ||
| 240 | } else if (type === 2) { | ||
| 241 | var timeout; | ||
| 242 | } | ||
| 243 | return function () { | ||
| 244 | let context = this; | ||
| 245 | let args = arguments; | ||
| 246 | if (type === 1) { | ||
| 247 | let now = Date.now(); | ||
| 248 | 212 | ||
| 249 | if (now - previous > wait) { | ||
| 250 | func.apply(context, args); | ||
| 251 | previous = now; | ||
| 252 | } | ||
| 253 | } else if (type === 2) { | ||
| 254 | if (!timeout) { | ||
| 255 | timeout = setTimeout(() => { | ||
| 256 | timeout = null; | ||
| 257 | func.apply(context, args) | ||
| 258 | }, wait) | ||
| 259 | } | ||
| 260 | } | ||
| 261 | } | ||
| 262 | } | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 213 | export function debounce(func, wait, immediate) { | ||
| 214 | return function () { | ||
| 215 | let context = this; | ||
| 216 | let args = arguments; | ||
| 217 | |||
| 218 | if (debounceTimeout) clearTimeout(debounceTimeout); | ||
| 219 | if (immediate) { | ||
| 220 | var callNow = !debounceTimeout; | ||
| 221 | debounceTimeout = setTimeout(() => { | ||
| 222 | debounceTimeout = null; | ||
| 223 | }, wait) | ||
| 224 | if (callNow) func.apply(context, args) | ||
| 225 | } else { | ||
| 226 | debounceTimeout = setTimeout(function () { | ||
| 227 | func.apply(context, args) | ||
| 228 | }, wait); | ||
| 229 | } | ||
| 230 | }(); | ||
| 231 | } | ||
| 232 | |||
| 233 | /** | ||
| 234 | * @desc 函数节流 | ||
| 235 | * @param func 函数 | ||
| 236 | * @param wait 延迟执行毫秒数 | ||
| 237 | * @param type 1 表时间戳版,2 表定时器版 | ||
| 238 | * 时间戳版的函数触发是在时间段内开始的时候,而定时器版的函数触发是在时间段内结束的时候。 | ||
| 239 | */ | ||
| 240 | let throttleTimeout; | ||
| 241 | let throttlePrevious; | ||
| 242 | |||
| 243 | export function throttle(func, wait, type) { | ||
| 244 | if (type === 1) { | ||
| 245 | throttlePrevious = 0; | ||
| 246 | } else if (type === 2) { | ||
| 247 | throttleTimeout = 0; | ||
| 248 | } | ||
| 249 | return function () { | ||
| 250 | let context = this; | ||
| 251 | let args = arguments; | ||
| 252 | if (type === 1) { | ||
| 253 | let now = Date.now(); | ||
| 254 | |||
| 255 | if (now - throttlePrevious > wait) { | ||
| 256 | func.apply(context, args); | ||
| 257 | throttlePrevious = now; | ||
| 258 | } | ||
| 259 | } else if (type === 2) { | ||
| 260 | if (!throttleTimeout) { | ||
| 261 | throttleTimeout = setTimeout(() => { | ||
| 262 | throttleTimeout = null; | ||
| 263 | func.apply(context, args) | ||
| 264 | }, wait) | ||
| 265 | } | ||
| 266 | } | ||
| 267 | }(); | ||
| 268 | } | ||
| 269 | |||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or sign in to post a comment