Limiting function invocations using closures
This is called a debounce function, and you are doing it ok.

Here are some small improvements I can think of.  Nothing big:


Initialise lastInvokedTimestamp, so that it'll never be undefined.
Cache current time.
Track the threshold of next call instead, so that it becomes one addition per interval instead of one subtraction per entrance.
Pass arguments into the debounced function.
Simplify test cases's setTimeout.




function rateLimit ( func, interval ) {
  var nextInvokeTimestamp = 0;

  return function(){
    var now = Date.now();
    if ( now &lt; nextInvokeTimestamp  ) {
      return console.log('too early!');
    }
    nextInvokeTimestamp = now + interval;
    console.log('go ahead!');
    return func.apply( this, arguments );
  };

}

// run test cases
function logHello( message ){
  console.log( message || 'Hello!' ); 
}
limitedDoSomething = rateLimit( logHello, 500 );
limitedDoSomething( "Welcome!" ); // should log “Welcome!”
setTimeout( limitedDoSomething, 400 );  // should not log “Hello!"
setTimeout( limitedDoSomething, 500 );  // should log “Hello!"




You may notice that David Walsh is doing it quite differently.


Using setTimeout defers the actual call to a later event loop, instead of current event, to let things settle down a bit.
Because of the delay (which can be as low as 0, i.e. 4ms), he can call with the latest arguments in the interval, for example the latest mouse move. A bigger interval lets you catch more calls, with a slower respond.


It is not better or worse; it's just a different speed bump for different needs.
If your requirement is simply "reduce multiple calls into one", you are doing fine.