问题 underscore.js:_.throttle(函数,等待)


根据 下划线文件

throttle_.throttle(功能,等待)
  创建并返回一个新的,   被调用的传递函数的版本,当被调用时   反复,实际上只会调用原始函数一次   每等待几毫秒。对于速率限制事件很有用   发生得比你能跟上的要快。

这是什么意思 Useful for rate-limiting events that occur faster than you can keep up with
这个函数相当于setTimeout,它有一个调用自身的函数?
有人可以在jsfiddle上提供一些例子吗?


12381
2018-05-01 08:18


起源

它很有用,例如用于滚动或调整大小事件处理程序,否则在滚动或调整窗口大小时,这些处理程序通常会在大多数情况下被触发。 - Niko


答案:


它不仅仅是setTimeout() 尝试这个

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

它将每秒调用一次,而不是每次迭代调用一次。 在本机JS中它看起来像:

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

不完全相同,只是为了说明函数被调用一次


9
2018-05-01 08:32



你不应该使用 while(true) - andlrc
这是 debounce不是 throttle因为只要在清除定时器之前调用它就永远不会触发。 - vsync


扩展 达哈泽的回答

它更像是,除了_.throttle紧急调用然后再调用 delay 毫秒

function throttle(func, delay) {
    var timer = 0;

    return function() {
        var context = this,
            args = [].slice.call(arguments);

        clearTimeout(timer);
        timer = setTimeout(function() {
            func.apply(context, args);
        }, delay);
    };
}

4
2018-01-14 06:34



这也是一个 debounce 功能......不是 throttle 一。 drupalmotion.com/article/... - vsync


我发现这个优秀的jsfiddle帮助了我:

http://jsfiddle.net/amyseqmedia/dD99u/37/

在我的情况下,我需要节流,因为一个函数(这是一个服务器请求)在1秒内被调用大约500次,并且正在使服务器超载。所以我改变它,以便该函数只能被称为max 一旦 每3秒钟。所以它被调用的次数并不重要,它最多只会每3秒发生一次。

像这样的东西:

var informationFromServer;
var a = _.throttle(function(){
    informationFromServer = serverCallFunction();
}, 3000);

function getsCalledALot()
{
    a();
}

function serverCallFunction()
{
    var data = $.post....
    return data;
}

1
2017-10-08 23:02



同意, jsfiddle.net/amyseqmedia/dD99u/37 太棒了! - jbobbins
小提琴它不再起作用了。 - Flame_Phoenix


此处描述了油门和去抖动之间的区别: https://css-tricks.com/the-difference-between-throttling-and-debouncing/

/*
"Debouncing enforces that a function not be called again until a certain amount of time has passed without it being called. As in "execute this function only if 100 milliseconds have passed without it being called."
"Perhaps a function is called 1,000 times in a quick burst, dispersed over 3 seconds, then stops being called. If you have debounced it at 100 milliseconds, the function will only fire once, at 3.1 seconds, once the burst is over. Each time the function is called during the burst it resets the debouncing timer."
*/
_.debounce = (fn, delay) => {
  let timer
  return (...args) => {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      fn.apply(null, args)
    }, delay)
  }
}
/*
"Throttling enforces a maximum number of times a function can be called over time. As in "execute this function at most once every 100 milliseconds."
 */
_.throttle = (fn, delay) => {
  let canCall = true
  return (...args) => {
    if (canCall) {
      fn.apply(null, args)
      canCall = false
      setTimeout(() => {
        canCall = true
      }, delay)
    }
  }
}

1
2017-07-04 20:08





_。风门 用于防止频繁调用特定ms.Refer映像的方法来理解这一点 RestrictfrequentCall.jpg


0
2017-09-22 08:11