function debounce (fn,time) {
let timer = null;
// ...args
// let fn2 = fn
return (...args)=>{
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
// fn.apply(this, args)
fn()
}, time)
}
}
function fn(){
console.log(123456)
}
const call = debounce(fn,1000)
// function debounce(fn, wait = 50) {
// // 通过闭包缓存一个定时器 id
// let timer = null
// // 将 debounce 处理结果当作函数返回
// // 触发事件回调时执行这个返回函数
// return function(...args) {
// // 如果已经设定过定时器就清空上一次的定时器
// if (timer) clearTimeout(timer)
// // 开始设定一个新的定时器,定时器结束后执行传入的函数 fn
// timer = setTimeout(() => {
// fn.apply(this, args)
// }, wait)
// }
// }
// 执行 debounce 函数返回新函数
// const betterFn = debounce(() => console.log('fn 防抖执行了'), 1000)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<button onclick="call()">点击</button>
<script src="./js/index.js"></script>
</body>
</html>