Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

防抖和节流的应用场景有哪些 #33

Open
suukii opened this issue Jul 27, 2020 · 0 comments
Open

防抖和节流的应用场景有哪些 #33

suukii opened this issue Jul 27, 2020 · 0 comments

Comments

@suukii
Copy link
Owner

suukii commented Jul 27, 2020

防抖

防抖是为了避免把一次事件误认为是多次,重在清零(clearTimeout),单位时间内事件重复触的话计时器会被重置。

  • 登录、发信息等按钮避免用户点击太快导致发送了多次请求,需要防抖
  • 调整浏览器窗口大小时,为避免 resize 事件触发过于频繁造成计算过多,需要用到防抖
  • 文本编辑器实时保存时,当无任何更改操作一秒后进行保存,这个需求可以用防抖来实现

节流

节流是控制事件发生的频率,比如控制事件发生频率为 1s,那么即使在 1s 内多次触发了事件,我们都当作它只发生了 1 次,单位时间内事件重复触发的话也只会被处理一次。

  • scroll 事件,使用节流每隔 1 s 才计算位置信息之类的
  • 浏览器播放事件,每秒进行一次进度信息的计算
  • input 框实时搜索并发送请求展示下拉列表,每隔 1 s 发送一次请求(也可以做防抖)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant