函数防抖和截流
js
export default {
/**
* fun防抖
* 指定时间后触发
*/
debounce(fun, wait = 1000) {
let timer
return function() {
const context = this
const args = arguments
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
fun.apply(context, args)
}, wait)
}
},
/**
* fun截流
* 指定时间内不再触发
*/
throttle(fun, wait = 1000) {
let timer
return function() {
if (timer != null) return
const context = this
const args = arguments
fun.apply(context, args)
timer = setTimeout(() => {
timer = null
}, wait)
}
}
}
export default {
/**
* fun防抖
* 指定时间后触发
*/
debounce(fun, wait = 1000) {
let timer
return function() {
const context = this
const args = arguments
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
fun.apply(context, args)
}, wait)
}
},
/**
* fun截流
* 指定时间内不再触发
*/
throttle(fun, wait = 1000) {
let timer
return function() {
if (timer != null) return
const context = this
const args = arguments
fun.apply(context, args)
timer = setTimeout(() => {
timer = null
}, wait)
}
}
}