JS image stabilization function expansion function

Smooth scrolling to the top of the page

const scrollToTop = () => { const c = document.documentElement.scrollTop || document.body.scrollTop; if (c > 0) { window.requestAnimationFrame(scrollToTop); window.scrollTo(0, c - c / 8); } } // 事例 scrollToTop()

/**

Anti-shake function


@Param {} function FUNC *
* @param {Number} execute the wait delay milliseconds
* @param {boolean} immediate true table immediate execution, false non-immediate execution table
* /
Export function Debounce (FUNC, the wait, immediate) {
the let timeout ;
return function () {
the let = the this context;
the let args = arguments;

if (timeout) clearTimeout(timeout);
if (immediate) {
let callNow = !timeout;
timeout = setTimeout(() => {
timeout = null;
}, wait);
if (callNow) func.apply(context, args)
}
else {
timeout = setTimeout(() => {
func.apply(context, args)
}, wait);
}
}
}

 

/**

Throttle function

 


* @Param {function} func function
* @param {number} wait delay the execution of several milliseconds
* @param {number} type 1 version stamp table, Table 2 Timer Version
* /
Export function Throttle (FUNC, the wait, type) {
Previous the let, timeout;
IF (type ===. 1) {
Previous = 0;
} the else IF (type === 2) {
timeout = null;
}
return function () {
the let = the this context;
the let args = arguments;
IF (type ===. 1) {
the let Date.now = now ();

if (now - previous > wait) {
func.apply(context, args);
previous = now;
}
}else if(type===2){
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args)
}, wait)
}
}

}
}

Guess you like

Origin www.cnblogs.com/wkk2020/p/12515127.html
Recommended