//这个防抖应用在 查询按钮,在1秒内多次点击只触发一次,防止频繁发起请求。
<body>
<button id="btn">点击</button>
</body>
<script>
//防抖函数
function debounce(fn, delay) {
let timer = null;
return function () {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this);
}, delay);
}
}
function fn1(){
console.log('哈哈哈');
//可以把异步数据请求写在这里
}
let btn =document.getElementById("btn");
btn.οnclick=debounce(fn1,1000)
</script>
这里是源生js在vue中就更简单啦,你学会了吗?