//防抖函数
//当事件触发多次时,delay后只执行最后一次
function debounce (fn,delay) {
var timer;
return function() {
var that = this;
var args = arguments;
if(timer) {
clearTimeout();
}
timer = setTimeout(function(){
fn.apply(that,args)
},delay);
}
}
//节流函数
//采用闭包,规定每隔delay的时间执行一次
function throttle (fn,delay) {
var last = 0;
return function () {
var arus = arguments;
var now = Date.now()
var that = this;
if(now-last>delay) {
fn.apply(that,arus)
last = now;
}
}
}
简单的防抖节流函数(JavaScript)
最新推荐文章于 2022-03-30 21:53:03 发布