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