// 节流,每隔一段时间执行一次
function throttle(fn, wait) {
var timer = null;
return function() {
if (!timer) {
timer = setTimeout(() => {
fn.apply(this, Array.prototype.slice.call(arguments, 0));
timer = null;
}, wait);
}
}
}
适用场景,监听页面的滚动事件而触发一些动作
// 防抖,只执行最后一次
function debounce(fn, wait) {
var timer = null;
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, Array.prototype.slice.call(arguments, 0)), wait);
}
}
适用场景,根据用户在输入框的输入查询后台数据