export function debounce(fun, delay = 400) {
// 定时器容器
let timer = null
// 返回一个函数
return (...args) => {
// 清除定时器 可以保证该函数如果多次连续单位400ms毫秒内调用 ,只执行一次
clearTimeout(timer)
// 定时器
timer = setTimeout(() => {
fun.call(this, ...args)
}, delay);
}
}
然后
import { debounce } from './utils/debounce'
const App = () => {
const debounce(headClick()=>{
console.log(1)
// ····
},1000)
// 1000等于一秒,一秒内只执行一次这个事件
return <button onclick={()=> headClick() }>点我</button>
}
export default App
感谢各位的大大的关注和点赞。如果有什么问题请在下方留言,我会及时改正。