JS中函数节流的使用

JS中的函数节流:

      在JS中的函数大多数情况下都是由用户主动触发调用的,除非是函数本身不合理,一般我们不会遇到与性能相关的问题。但是在一些少数情况下,函数的触发不是由用户直接控制的。在这些场景下,函数非常有可能被频繁的地调用,从而造成大的性能问题。

     1.1函数被频繁调用的场景:

          1.当我们给window对象绑定了resize事件的时候,当浏览器的窗口大小改变的时候,这个事件的触发频率就非常高了,假如我们在window.onresize事件函数中有一些跟DOM节点相关的操作,而DOM节点相关的操作往往是非常消耗性能的,就可能会造成浏览器卡顿的现象。

        2.同样,如果我们给div节点绑定了一个拖拽事件(比如mousemove),当div节点被拖动的时候,也会频繁地触发该拖拽事件函数。

上述例子是在使用js开发时可能会遇到的场景,当在开发时如果出现上述的情况就会造成性能的问题,所以,为了解决上述问题,我们引入了函数节流的概念。

     1.2函数节流的原理:

            我们观察上述提到的例子,可以整理出一个共同点,就是函数被触发的频率太高了。比如说当我们通过拖拽来改变窗口大小的时候,打印窗口大小的工资在一秒钟内进行了十次,但是我们实际上只需要两次或者三次。这就需要我们按时间段来忽略掉一些事件请求,显然我们可以借助setTimeout来实现。

     1.3函数节流的实现:

           如下所示,有这样一个代码片段:

<body>
    <div style="height: 500px; width: 300px; background-color: rgb(244, 199, 207);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(239, 131, 16);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(11, 66, 194);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(177, 21, 244);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(75, 180, 115);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(163, 122, 150);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(39, 34, 35);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(209, 218, 40);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(63, 179, 215);"></div>
    <script  >
       function handler(){
               console.log('页面发生了滚动');
       }
       document.addEventListener('scroll',handler);
    </script>
</body>

当我们不添加节流函数时,只要页面一滚动,控制台就会输出

这样在开发过程中就会出现一些性能上的问题,当我们使用函数节流来优化时,一般有两种思路。

 1.通过时间戳来实现:
<body>
    <div style="height: 500px; width: 300px; background-color: rgb(244, 199, 207);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(239, 131, 16);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(11, 66, 194);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(177, 21, 244);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(75, 180, 115);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(163, 122, 150);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(39, 34, 35);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(209, 218, 40);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(63, 179, 215);"></div>
    <script  > 
//传递两个参数,一个是要执行的程序,一个时设置的延迟时长
   function throttle(func,delay){
//存储上一次函数执行的时间戳,初始值为0
    var start=0;
    return function(){
      var that=this;
      var args=arguments;
      var current=Date.now();
//判断前后两次执行的差值是否大于等于设置的延迟时长
      if(current-start>=delay){
        func.call(that,args)
        start=current;
      }
    }
   }
   function handler(){
               console.log('页面发生了滚动');
       }
       document.addEventListener('scroll',throttle(handler,1000));
   </script>
</body>

如上所示,就是通过时间戳来实现函数节流的方式,通过上述的代码,可以实现延迟1000ms再去执行函数。

 2.使用定时器来实现:
<body>
    <div style="height: 500px; width: 300px; background-color: rgb(244, 199, 207);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(239, 131, 16);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(11, 66, 194);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(177, 21, 244);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(75, 180, 115);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(163, 122, 150);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(39, 34, 35);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(209, 218, 40);"></div>
  <div style="height: 500px; width: 300px; background-color: rgb(63, 179, 215);"></div>
    <script  > 
  
  //定时器写法
  function throttle(func,delay){
    var timer=null;
    return function(){
      var that=this;
      var args=arguments
      if(!timer){
        timer=setTimeout(function(){
          func.call(that,args)
          timer=null;
        },delay)
      }
    }
  }
   function handler(){
               console.log('页面发生了滚动');
       }
       document.addEventListener('scroll',throttle(handler,1000));
   </script>
</body>

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: JavaScript 函数节流是一种优化技术,旨在通过限制函数的执行频率来提高性能。通常,当用户在界面上进行操作时,会触发大量的函数调用,比如拖动滑块、滚动屏幕等。如果每次调用都执行函数的全部代码,可能会造成性能问题。因此,我们可以使用函数节流来限制函数的执行频率,从而提高性能。 下面是一个简单的函数节流实现: ``` function throttle(func, wait) { let timeout; return function() { const context = this; const args = arguments; if (!timeout) { timeout = setTimeout(() => { timeout = null; func.apply(context, args); }, wait); } } } ``` 使用方法: ``` const throttledFunction = throttle(function() { // 这里是函数的代码 }, 1000); ``` 在上面的代码,我们定义了一个名为 throttle 的函数,它接收两个参数:要节流函数和等待时间。throttle 函数返回一个新的函数,这个新函数在被调用时会延迟执行原函数,直到等待时间过去后才真正执行。 使用函数节流可以有效减少函数调用的次数,从而提高性能。但是要注意,函数节流会导致函数的响应时间变长,所以在使用时要根据具 ### 回答2: JavaScript 函数节流是一种控制函数执行频率的方法。通过节流,我们可以限制函数的调用频率,从而减少函数的执行次数,提高性能。 函数节流的原理是创建一个计时器,在指定的时间间隔内只允许函数执行一次,如果在时间间隔内多次触发函数执行,只有一次会被执行,其余的会被忽略。 下面是一个简单的实现函数节流的例子: ```javascript function throttle(func, delay) { let timer = null; return function () { if (!timer) { timer = setTimeout(() => { func.apply(this, arguments); timer = null; }, delay); } }; } ``` 在上述代码,`throttle` 函数接收两个参数:`func` 表示需要节流函数,`delay` 表示时间间隔,单位是毫秒。 `throttle` 函数内部定义了一个`timer`变量,用于保存计时器的标识。在函数被调用时,先判断`timer`是否为`null`,如果为`null`,则创建一个计时器,并在指定的时间间隔后触发函数执行。执行函数时,将使用 `apply` 方法将传入的参数原封不动地传递给函数。 如果在时间间隔内多次触发函数执行,即使函数被调用,也不会创建新的计时器,保证函数的执行频率不超过设定的时间间隔。 通过使用函数节流,我们可以有效地控制函数的执行频率,提高页面的性能和用户体验。比如在监听滚动事件、鼠标移动事件等场景下,可以使用函数节流避免频繁调用函数,减少不必要的计算和资源消耗。 ### 回答3: JavaScript 函数节流是一种控制函数执行频率的技术,用于限制函数在一定时间内被触发的次数。 节流的实现原理是通过设置一个时间间隔,在这个时间间隔内只能触发一次函数执行,如果在时间间隔内再次触发函数执行,则需要等待时间间隔结束后才能再次执行。这样可以有效地控制函数的执行频率,避免函数被频繁调用造成性能损耗。 在实际应用,可以使用定时器来实现函数节流。具体的步骤如下: 1. 定义一个变量来存储函数最后一次执行的时间戳。 2. 在函数执行之初,获取当前的时间戳,并与上一次执行的时间戳进行比较。 3. 如果时间间隔大于设定的阈值,则执行函数,并更新最后一次执行的时间戳。 4. 如果时间间隔小于设定的阈值,则不执行函数,等待下次触发函数执行。 5. 使用定时器循环检测是否满足执行条件。 通过函数节流可以有效地降低函数的执行频率,减少不必要的计算和资源消耗。在监听滚动事件、窗口大小改变等频繁触发函数的场景下,使用函数节流可以提升页面性能和用户体验。 需要注意的是,在设置时间间隔时,要根据实际需求和函数的执行时间来合理设定,以避免函数不能及时响应用户操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值