Implementing Throttle Function

What is a throttle function. "throttle" function returns a function which will run only once with in the time specified. This is very useful when we wish to perform some operation only once even when the function is called multiple times. For more details, please follow the link.

In this article we are discussing how can we implement our own throttle method. The definition of our function should be

function throttle(fn, timeInMilliseconds){
    return function(){
      ...
    }
}

So. let's create a boolean variable which will in closure here, and change it only when the function is called or after certain time limit.

function throttle(fn, timeInMiliseconds){
  var t = true;

  return function(){
    if(t){
      fn();
      t = false;

      setTimeout(function(){
        t = true;
      },timeInMiliseconds);
    }
  }
}

Testing our throttle function

function hello(){
  console.log('hello');
}

//This returns a function which will only run once in 5 seconds.
var throttled = throttle(hello, 5000);

throttled(); //print hello
throttled(); //5 second isn't over yet

setTimeout(function(){
  throttled();
}, 2000); // 5 second isn't over yet

setTimeout(function(){
  throttled();
}, 6000); //print hello