第432天 说说防止重复发送ajax请求的方法有哪些?各自有什么优缺点? [3+1官网](http://www.h-camel.com/index.html) [我也要出题](http://www.h-camel.com/contribution.html)
Activity
longhui520 commentedon Jun 21, 2020
yangyi1987 commentedon Oct 6, 2020
// 方法一 防抖
function debounce(f, ms) {
let time;
return function(){
let arg = Array.prototype.slice.call(arguments, 1);
if(time) {
clearTimeout(time);
}
time = setTimeout(function(){
f.apply(this, arg)
},ms)
}
}
// 方法二 节流
function throttle(f, ms){
let lastTime = 0;
return function(){
let arg = Array.prototype.slice.call(arguments, 1);
let newTime = Date.now();
if(newTime-lastTime > ms) {
setTimeout(function(){
f.apply(this, arg)
},ms)
}
lastTime = newTime;
}
}