/*
* @Author: beth.miao
* @Date: 2018-08-03 17:38:15
* @Last Modified by: beth.miao
* @Last Modified time: 2018-08-03 18:04:42
*/
// 简版
function debounce(fn, interval = 300) {
let timeout = null; // timeout占位
return function () { // 返回闭包函数 timeout保留作用域
clearTimeout(timeout); // 先清除上一个timeout
timeout = setTimeout(() => { //重新给timeout赋值,interval时间之后执行cb
fn.apply(this, arguments);
}, interval);
};
}
function logTime(){
console.log(new Date().getTime());
}
// 控制台测试
document.addEventListener('scroll',debounce(logTime, 1000))
复制代码
debounce
最新推荐文章于 2024-06-17 15:39:12 发布