Kaynağa Gözat

添加防抖和节流函数

tags/1.13.2
Caven Chen 4 yıl önce
ebeveyn
işleme
35bf1f4690
1 değiştirilmiş dosya ile 34 ekleme ve 0 silme
  1. 34
    0
      src/core/utils/Util.js

+ 34
- 0
src/core/utils/Util.js Dosyayı Görüntüle

position.hasOwnProperty('_alt') 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 export default Util

Loading…
İptal
Kaydet