SOURCE

console 命令行工具 X clear

                    
>
console
let body = document.body;

function showTop() {
    var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
    console.log("滚动条位置" + screenTop)
}

// window.onscroll = debounce(showTop, 500)

window.onscroll = throttle(showTop, 500)


// 防抖只需要最后一次就行
function debounce(fn, delay) {
    let timer = null 
    return function () {
        if (timer) {
            clearTimeout(timer) 
            timer = setTimeout(fn, delay)
        } else {
            timer = setTimeout(fn, delay) 
        }
    }
}


// 节流就是我想要的频率不需要那么快,但是在规定时间必须执行一次
function throttle(fn, delay) {
    let flag = true;
    return function () {
        if (flag) {
        setTimeout(function () {
            fn()
            flag = true;
        }, delay) 
        }
        flag = false
    }
}
<div id="container">
    
</div>
#container{
    height: 2000px;
    width: 100px;
    background-color: black;
}