function testFn() { console.log(1111); }
var throttle = function (fn, delay,atleast) {
var timer = null;
var pretime = null;
return function () {
var now = Date.now()
if(!pretime) pretime = now;
if(now - pretime >atleast){
fn();
pretime = now;
}else{
clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
};
throttle(testFn, 200,1000)()
Throttle 小例子
最新推荐文章于 2022-04-19 09:31:07 发布