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

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

 

posted @ 2019-06-03 16:53  数学系的挨踢者  阅读(406)  评论(0编辑  收藏  举报