码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-06-03 17:19:14      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:func   div   ast   throttle   fun   ons   nts   调用   限制   

<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>

 

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

标签:func   div   ast   throttle   fun   ons   nts   调用   限制   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!