-
-
Save msssk/b720a8bddf2ba595347820ac387751ce to your computer and use it in GitHub Desktop.
// This is a typical throttle implementation with the difference that it doesn't discard | |
// the final invocation - instead it runs it at the next valid timeslice. | |
throttleBoss: function(callback, delay) { | |
var ready = true, | |
args = null; | |
return function throttled() { | |
var context = this; | |
if (ready) { | |
ready = false; | |
setTimeout(function() { | |
ready = true; | |
if (args) { | |
throttled.apply(context); | |
} | |
}, delay); | |
if (args) { | |
callback.apply(this, args); | |
args = null; | |
} else { | |
callback.apply(this, arguments); | |
} | |
} else { | |
args = arguments; | |
} | |
}; | |
} |
Your comment isn't entirely correct - throttleAfter
would not fire at 0.
Unless I'm missing something, I don't see how this changes timing other than that whatsoever (which your comment also seems to suggest). This is basically throttle plus throttleAfter in behavior. The main differences between this and throttleAfter
are:
- This will call the function immediately upon first invocation - including if that's the only invocation (whereas
throttleAfter
would still wait the full interval). - This properly passes the last invocation's arguments to the final delayed call.
throttleAfter
should probably do that but doesn't, I think I'd consider that a bug.
I think we would need a better name for this (and I don't think my suggestion of throttleSandwich
will stick, either), and I have to wonder if it can be simplified at all.
One significant issue with this implementation is that it won't work for throttled functions that receive no arguments.
With an implementation that fixes this issue, my vote is we simply call it throttle
. Is there reason to have both this implementation and the existing throttle
implementation?
Goal: a throttle method for rapid event handlers, such as mousemove
and scroll
.
Presumed desirable traits:
- Responsive: execute the first invocation without delay; execute other eligible invocations with minimal delay
- Accurate: ensure that trailing invocations during the timeout period are not discarded when the event ceases to be triggered
- Performant: of course, throttling should happen for performance, also the manner of throttling should incur minimal overhead
throttle
Trailing invocations are discarded
01234512345123
0 5 5
throttleAfter
Initial invocation and trailing invocations are discarded
1234512345123
5 5
throttleBoss
Final trailing invocation executes at next eligibile timeslice
01234512345123
0 5 5 3
Comparison with Dojo's
throttleAfter
: