对函数防抖以及函数节流的封装
文章目录
对函数防抖的封装
function debounce(fn, delay) {
let timeId = null;
return function () {
//修改this指向
let that = this;
let args = arguments;
timeId && clearTimeout(timeId);
timeId = setTimeout(function () {
fn.apply(that,args)
},delay || 1000);
}
}对函数节流的封装
function throttle(fn, delay) {
let timeId = null;
let flag = true;
return function () {
if (!flag) return
flag = false
let that = this;
let args = arguments;
timeId && clearTimeout(timeId);
timeId = setTimeout(function () {
flag = true
fn.apply(that,args);
},delay || 1000);
}
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论