防抖(参数也要指定)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<button>点击按钮</button>
<script>
function debounce(callback, delay) {
return function (event) {
if (callback.timeoutId) {
clearTimeout(callback.timeoutId)
}
callback.timeoutId = setTimeout(() => {
console.log(event)
callback.call(event.target, event)
delete callback.timeoutId
}, delay)
}
}
const button = document.querySelector('button')
const that = this
let aa = () => {
console.log('请点击我')
}
button.addEventListener('click', debounce(aa, 2000))
</script>
</body>
</html>