当前位置:网站首页>防抖和节流
防抖和节流
2022-04-23 06:26:00 【笔描相思】
防抖:解决的问题就是在一段时间间隔内多次触发事件的时候,只会执行一次
节流:解决的问题是减少一段时间的触发频率
1:函数在指定时间内只会触发一次,具体实现方法
第一次触发函数的时候,延迟delay时间执行,如果在delay时间段内再次触发该函数,则重新开始
计时
如果delay时间段内没有触发该函数,则执行该函数
function bunde(fn ,depaly)
{
let timetar=null;
return function()
{
if(timetar)
{
clearTimeout(timetar);
}
timetar=setTimeout(fn,depaly);
}
}
节流:
防抖的问题是,在短时间内不断触发事件,回调函数永远不会执行。
节流的思想:在短时间内不断触发事件,回调函数只会在指定间隔时间内执行。
function throttle(fn, delay) {
let timer = null;
return function() {
if (timer) return false
timer = setTimeout(() => {
fn()
timer = null
}, delay)
}
}
版权声明
本文为[笔描相思]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_44788119/article/details/120902466
边栏推荐
猜你喜欢
随机推荐
SAP CR传输请求顺序、依赖检查
F-牛妹的苹果树(直径合并)
Moment.js中format方法函数的格式
14.事务处理
3. Sort statement
MySQL index
莫比乌斯反演
Implementation of MySQL persistence
1.查看数据库和表
Common DOS commands
驼峰命名对像
【自我激励系列】你永远不会准备好
[Ted series] how does a habit change my life
keytool: command not found
ABAP 从CDS VIEW 发布OData Service示例
Game assisted script development journey
ogldev-读书笔记
js之节点操作,为什么要学习节点操作
[self motivation series] what really hinders you?
[CodeForces - 208E] Blood Cousins(k代兄弟问题)