前端节流
2019-11-13 本文已影响0人
Devin_tao
前端节流(规定时间内只出发一次,规定时间内再次触发则无效)
// 节流
function throttle (fn , delay) {
var prev = Date.now();
return function () {
var now = Date.now();
if (now - prev >= delay) {
fn.apply(this, arguments);
prev = Date.now();
}
}
}
function foo() {
console.log(1);
}
addEventListener('scroll', throttle(foo, 1000));