1 min readSep 6, 2017
Hey lolzery wowzery
I’m glad you found a solution that works for you! However, I’m not sure I understand where the perceived overhead comes from.
var throttle = function(func, limit) {
var inThrottle,
lastFunc,
lastRan;
return function() {
var context = this,
args = arguments;
if (!inThrottle) {
func.apply(context, args);
lastRan = Date.now()
inThrottle = true;
} else {
clearTimeout(lastFunc)
lastFunc = setTimeout(function() {
if ((Date.now() - lastRan) >= limit) {
func.apply(context, args)
lastRan = Date.now()
}
}, limit - (Date.now() - lastRan))
}
};
};
IMHO there is less going on in this throttle
function as a solution that makes it more usable in different scenarios. It may not always be the case that you want to throttle
off of user interaction.
Either way, I’d be interested to see the overhead comparison in a perf test.
Thanks for sharing your solution! 😀 👍