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

javascript, underscore.js

Solution

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

Problem

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

Original source