当前位置:网站首页>Handwritten universal anti chattering and throttling function
Handwritten universal anti chattering and throttling function
2022-04-22 06:44:00 【Zhou zhouchi】
We often bind some continuously triggered events , such as resize、scroll、mousemove wait , If the event call is unlimited , Will increase the burden on browsers , It leads to poor user experience , We can use debounce( Shake proof ) and throttle( throttle ) To reduce frequent calls , At the same time, it will not affect the actual effect .
The concept of anti shake
After the event is triggered n Seconds to execute the function , If in n The event is triggered in seconds , The function execution time is recalculated
The anti shake function can be divided into immediate execution , And non immediate execution
Straight white : For a certain period of time , Only the last operation is valid , In another wait Ms before the function is executed
Use scenarios : For example, when dragging to change the size of the browser , Need to change after When the event is triggered again , You can use anti shake .
// Handwriting JS General anti shake function
let timeout = null;
/** * Anti shake principle : For a certain period of time , Only one last operation , In another wait Ms before the function is executed * * @param {Function} func The callback function to execute * @param {Number} wait Delay time * @param {Boolean} immediate Whether to execute immediately * @return null */
const debounce = function(func, wait = 500, immediate = false) {
if (timeout !== null) clearTimeout(timeout);
if (immediate) {
return (...args) => {
let callNow = !timeout;
timeout = setTimeout(() => {
timeout = null
}, wait);
if(callNow) typeof func === 'function' && func.call(undefined, ...args)
}
}
else {
return (...args) => {
timeout = setTimeout(() => {
typeof func === 'function' && func.call(undefined, ...args)
}, wait);
}
}
};
const d = (name) => {
console.log(' Execute function print ',name)
}
let d2=debounce(d,2 * 1000)
d2(' The Monkey King ')
The concept of throttling
Continuous triggering events , But in n Execute function only once per second .
Straight white : In a certain amount of time , Trigger only once
Use scenarios : For example, the user clicks the snap up button , Suppose that every 5 You can only effectively click once per second , Then you can use throttling . Throttling is usually used on buttons .
// Handwriting JS General throttle function
let timer;
/** * Throttling principle : In a certain amount of time , Trigger only once * * @param {Function} func The callback function to execute * @param {Number} wait Delay time * @param {Boolean} immediate Whether to execute immediately * @return null */
const throttle = function (func, wait = 500, immediate = true, context = undefined) {
if (immediate) {
return (...args) => {
if (timer) return
typeof func === 'function' && func.call(context, ...args)
timer = setTimeout(() => {
timer = null
}, wait)
}
}
else {
return (...args) => {
if (timer) return
timer = setTimeout(() => {
timer = null
typeof func === 'function' && func.call(context, ...args)
}, wait)
}
}
};
const d = (name) => {
console.log(' Execute function print ', name)
}
let d2 = throttle(d, 2 * 1000, true);
d2(' The Monkey King ')
版权声明
本文为[Zhou zhouchi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220557472111.html
边栏推荐
猜你喜欢
随机推荐
nodejs+Express+mongodb
指纹支付相关的细节处理
ES6 数组方法记录
从零开始学安卓(kotlin)一 ——入门
Difference between canvas and SVG
负载平衡的意义什么
JS judge the PC end or mobile end
uniapp 微信小程序用户授权获取当前位置信息 腾讯地图
创新实训(五)中期检查
Regular verification
通用型枚举常量类
redis存入数据显示乱码问题
JS five methods to judge the most complete data types
watch和computed的区别
创新实训(十)
Uniapp custom bottom navigation bar H5 has an effective solution to invalid applet
创新实训(七)FBV视图&CBV视图
uniapp生成canvas商品海报
pip3安装requests库-最全踩坑整理
Uniapp web View example (wechat applet)









