函数防抖~
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button id="btn">按钮</button>
<script>
function debounce(fn, delay) {
// 记录上一次的延迟时间
var timer = null
return function () {
// 先清除第一次的定时器
clearTimeout(timer)
timer = setTimeout(function () {
fn.apply(this)
}, delay)
}
}
// btn.addEventListener('click', console.log('ok'))
document.querySelector('#btn').onclick = debounce(function(){
console.log('kk')
},1000)
console.log(debounce())
</script>
</body>
</html>
具体可以参考以上代码~