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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10395227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:43:38  来源:igfitidea点击:

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

javascriptunderscore.js

提问by underscore666

According the underscore documentation:

根据下划线文档

throttle_.throttle(function, wait)
Creates and returns a new, throttled version of the passed function, that, when invoked repeatedly, will only actually call the original function at most once per every wait milliseconds. Useful for rate-limiting events that occur faster than you can keep up with.

Throttle_.throttle(function, wait)
创建并返回传递函数的新的节流版本,当重复调用时,每等待毫秒实际上最多只调用一次原始函数。对发生速度快于您跟不上的速率限制事件很有用。

What does it means Useful for rate-limiting events that occur faster than you can keep up with.
This function is equivalent to setTimeout with a function that calls itself?
Can someone provide me some example on jsfiddle?

这是什么意思Useful for rate-limiting events that occur faster than you can keep up with
这个函数等价于 setTimeout 带有调用自身的函数?
有人可以为我提供一些关于 jsfiddle 的例子吗?

回答by Maxim Krizhanovsky

it's not just setTimeout() Try this

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

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:

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

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

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

回答by andlrc

To extend Darhazer's answer

扩展Darhazer 的回答

It's more like, except _.throttle is called imminently and then again after delaymilliseconds

这更像是,除了 _.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);
    };
}

回答by Katie

I found this excellent jsfiddle that helped me:

我发现这个出色的 jsfiddle 对我有帮助:

jsfiddle.net/max23_/2wn5ybdg/1(updated by @max23_)

jsfiddle.net/max23_/2wn5ybdg/1(由@max23_ 更新)

In my case I needed throttle because a function (which was a server request) was being called about 500 times in 1 second, and was overloading the server. So I changed it so that the function could only be called max onceper 3 seconds. So it doesn't matter how many times it's called, it'll only occur once per 3 seconds max.

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

Something like this:

像这样的东西:

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

function getsCalledALot()
{
    a();
}

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

回答by susiepk

The difference between throttle and debounce is described here: https://css-tricks.com/the-difference-between-throttling-and-debouncing/

节流和去抖动之间的区别在这里描述: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)
    }
  }
}

回答by Aravinth Ramesh

_.throttleis used to prevent frequent calls on a method for a particular ms.Refer image to understand this RestrictfrequentCall.jpg

_.throttle用于防止对特定ms.Refer 图像的方法的频繁调用以了解此RestrictfrequentCall.jpg