防抖策略:当事件被触发后,延迟n秒再执行回调,如果在这n秒内事件又被触发,则重新计时;
// 1、定义延时器的ID
let timer = null;
// 2、定义防抖函数
function debounceSeach(kw) {
timer = setTimeout(() => {
phone();
}, 500);
}
btn.addEventListener('click', function () {
// 3、执行之前打断上一次的请求
clearTimeout(timer);
///4、然后再执行本次请求
debounceSeach();
})
function phone() {
axios({
method: 'post',
url: url,
data: {
text: "测试测试测试测试测试测试"
}
}).then(res => {
console.log(res.data);
})
}