Anti-shake and throttling

//Anti-shake 
/*
* Idea: When the scroll bar is scrolling, trigger the scroll event, here is set to execute the method once
in 5 milliseconds * If the event is triggered again in these 5 milliseconds, the last call
* debounce method will be deleted , delete the last call first, and then execute setTimeout
* The function of anti-shake ensures that the scroll event must be executed once at an interval of more than 5 milliseconds
. * */
window.onscroll=function () {
//lazyload()
debounce(lazyload,window )
}
function debounce(method,context) {
clearTimeout(method.timeout)
method.timeout = setTimeout(()=>{
method.call(context)
},500)
}
function lazyload () {
console.log('onscroll' )
}

throttling


function lazyload () {
console.log('onscroll')
}

var canCel = true
window.onscroll = function () {
if (!canCel) {
return
}
canCel = false
setTimeout(()=>{
throttle(lazyload,window)
canCel = true
},500)
}
function throttle(method,context) {
method.call(context)
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325619060&siteId=291194637