feat: 为防抖函数添加首次执行功能
This commit is contained in:
parent
78fa340090
commit
ec2e791df6
13
js/防抖.js
13
js/防抖.js
@ -23,12 +23,19 @@ export default function deBounce(fun, delay) {
|
|||||||
*/
|
*/
|
||||||
export default function deBounce(fun, delay) {
|
export default function deBounce(fun, delay) {
|
||||||
var timer = 0
|
var timer = 0
|
||||||
return function() {
|
var flag = true // 是否允许执行
|
||||||
|
return function(){
|
||||||
var args = arguments
|
var args = arguments
|
||||||
var context = this
|
var context = this
|
||||||
if(timer) clearTimeout(timer)
|
if(flag) {
|
||||||
|
fun.apply(context, args)
|
||||||
|
flag = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
clearTimeout(timer)
|
||||||
timer = setTimeout(function(){
|
timer = setTimeout(function(){
|
||||||
fun.apply(context, args)
|
fun.apply(context, args)
|
||||||
})
|
flag = true
|
||||||
|
},delay)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user