成都创新互联网站制作重庆分公司

js 防抖与节流

1. 概念上的区别(从 handle 的有效性分析)

  1. 防抖:多次执行只有最后一次生效,必要参数 [ handle, time ]
  2. 节流:一段时间内只能执行一次,必要参数 [ handle, time ]

2. 实现一下

  1. 防抖:
     1 function debounce(handle, time) {
     2     let timer = null;
     3     return function () {
     4         if (timer) {
     5             clearTimeout(timer);
     6             timer = null;
     7         }
     8         timer = setTimeout(() => {
     9             handle();
    10         }, time);
    11     };
    12 }

    文章名称:js 防抖与节流
    链接分享:http://cxhlcq.com/article/dsojicc.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部