1、第一种
debounce (func, delay) {
let timer = null
return function (...args) {
if (timer) clearTimeout(timer)
timer = setTimeout(() => {
func.apply(this, args)
}, delay)
}
}
2、第二种
<body>
<input type="text" id="input">
<script>
function debounce(fun,delay){
let timer;
return function(args){
clearInterval(timer);
timer = setTimeout(function(){
fun(args);
},delay)
}
}
function inputFun(value){
console.log(`你输入的结果是${value}`);
}
const input = document.getElementById("input");
const debounceInput = debounce(inputFun,1000);
input.addEventListener('keyup',function(e){
debounceInput(e.target.value);
})
</script>
</body>