underscore.js: _.throttle(function, wait)

前端 未结 5 1036
轮回少年
轮回少年 2021-02-19 13:21

According the underscore documentation:

throttle_.throttle(function, wait)
Creates and returns a new, throttled version of the pa

5条回答
  •  鱼传尺愫
    2021-02-19 13:39

    it's not just setTimeout() Try this

    var a = _.throttle(function(){console.log('called')}, 1000);
    while(true) {
      a();
    }
    

    it will be called once every second and not once every iteration. In native JS it would look like:

    var i = null;
    function throttle(func, delay){
      if (i) {
          window.clearTimeout(i);
      }
      i = window.setTimeout(func, delay)
    }
    

    not exactly the same, but just to illustrate that the function is called once

提交回复
热议问题