/**
write a function, throttle, that returns a function that run only once per n milliseconds.
The function should accept 3 parameters:
* the function to call
* a waite time (in millisecondss)
* and an optional scope to apply to the new function.
**/
var lastTime;
function throttle(callback, waittime, scope) {
var curTime = 0
, scp = scope || this;
return function() {
var ltime
, delay = curTime - ltime;
, lastInvoked;
if (delay >= waittime) {
lastInvoked = new Date().getMilliseconds();
callback.apply(scp, Array.prototype.slice.call(arguments));
}
else {
setTimeout(function() { throttle(callback, waittime, scope) }, delay);
}
lastTime = new Date().getMilliseconds();
};
}
var t_c = throttle( function( name ) { console.log( 'hello, ' + name ) }, 1500 );
t_c( 'ryan' );
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.