Browse Source

添加防抖和节流函数

tags/1.13.2
Caven Chen 4 years ago
parent
commit
35bf1f4690
1 changed files with 34 additions and 0 deletions
  1. 34
    0
      src/core/utils/Util.js

+ 34
- 0
src/core/utils/Util.js View File

@@ -120,6 +120,40 @@ class Util {
position.hasOwnProperty('_alt')
)
}

/**
* Creates a debounced function that delays invoking `fn` until after `delay`
* @param fn
* @param delay
* @returns {function(): void}
*/
static debounce(fn, delay) {
let timer = null
return function() {
timer && clearTimeout(timer)
timer = setTimeout(fn, delay)
}
}

/**
* Creates a throttled function that only invokes `fn` at most once per
* @param fn
* @param delay
* @returns {function(): void}
*/
static throttle(fn, delay) {
let valid = true
return function() {
if (!valid) {
return false
}
valid = false
setTimeout(() => {
fn()
valid = true
}, delay)
}
}
}

export default Util

Loading…
Cancel
Save