feat: 为防抖函数添加首次执行功能

This commit is contained in:
= 2024-05-06 14:58:49 +08:00
parent 78fa340090
commit ec2e791df6

View File

@ -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)
} }
} }