一、函数防抖
使用场景:文本框输入,设置延时,超时再执行请求
实现原理:事件被触发n秒后再执行,n秒内又被触发则重新计时
//common
function debounce(fn,wait = 500){
var timer;//计时器
return function(){
//----需要防抖的方法(fn)的参数----
var args = arguments;//fn方法对象(数组形式)
var that = this;
//------------------
clearTimeout(timer)//清除上次请求计时
timer = setTimeout(()=>{//计算请求时间,大于0.5s则发送请求
fn.apply(that,args)//接受数组形式参数
},wait);
}
}
module.exports = {
debounce: debounce
}
调用:
import { debounce } from ''
方法名: debounce(function(参数){
......
}),
//不要用箭头函数,this指向不对
方法名(){
debounce(function(参数){
......
})
},
原文链接:https://www.cnblogs.com/TigerZhang-home/p/11812386.html