underscore.js:_.throttle(函数,等待) [英] underscore.js: _.throttle(function, wait)
问题描述
根据下划线文档:
throttle_.throttle(函数,等待)
创建并返回传递函数的新的
限制版本,当调用$ b $时b重复,实际上每次等待几毫秒才会实际调用原始函数
。对于限制价格的事件非常有用,因为
出现的速度比你能跟上的速度要快。
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.
这是什么意思对于速度限制事件非常有用,它发生得比你能跟上
更快。
这个函数等同于setTimeout,它有一个调用自身的函数?
有人可以在jsfiddle上给我一些例子吗?
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?
推荐答案
这不仅仅是setTimeout()
试试这个
it's not just setTimeout() Try this
var a = _.throttle(function(){console.log('called')}, 1000);
while(true) {
a();
}
它将每秒调用一次,而不是每次迭代调用一次。
在原生JS中它看起来像:
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
这篇关于underscore.js:_.throttle(函数,等待)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!