//节流函数:连续触发事件时 n秒内只能执行一次处理函数。
function throttle(fn, delay) {
let previous = 0;//因为内部函数会调用这个变量,所以在函数执行完之后,这个变量不会被销毁
return function () {//节流的函数
let args = arguments;//argument是传入函数的参数
let now = new Date();
console.log(this)
if (now - previous > delay) {
// fn.apply(this, args);
fn()
previous = now;
}
}
}
function testThrottle() {
console.log("需要节流的函数");
}
let testThrottleFn = throttle(testThrottle, 2000); // 节流函数
testThrottleFn('popop', 'pl')
document.getElementById('btn').onclick = function () {
testThrottleFn('popop', 'pl')
}
节流函数(时间戳方式)
最新推荐文章于 2024-07-05 00:58:20 发布