函数防抖debounce、节流函数throttle
// 防抖
function debounce(fn, delay, ctx) {
let sto = null;
return function(){
let args = arguments;
sto && clearTimeout(sto);
sto = setTimeout(() => {
fn.apply(ctx, args);
}, delay);
}
}
// 节流
function throttle(fn, delay, ctx) {
let flag = true;
return function(){
let args = arguments;
if(flag) {
fn.apply(ctx, args);
flag = false;
setTimeout(function() {
flag = true;
}, delay)
}
}
}
window.onresize = throttle(function() {
console.log('test')
}, 1000, this)
本文标题:函数防抖debounce、节流函数throttle
本文链接:https://www.haomeiwen.com/subject/rydbmqtx.html
网友评论