30秒内限制函数只被调用一次

时间:2019-06-03 17:39:15   收藏:0   阅读:92
<script>
  function throttle (func, duration) {
      // duration 以秒计
      let last
      return function () {
           let now = Date.now()
           if (last && (now - last) < duration * 1e3) return
           last = now
           func.apply(this, arguments)
      }
  }

  var aa = function(a,b){
    console.log(a+b)      
  }

  var aa = throttle(aa, 30)

  aa(2, 3)
</script>

 

原文:https://www.cnblogs.com/wrongcode/p/10968581.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!