Skip to content

Instantly share code, notes, and snippets.

@msssk
Last active May 20, 2022 16:58
Show Gist options
  • Save msssk/b720a8bddf2ba595347820ac387751ce to your computer and use it in GitHub Desktop.
Save msssk/b720a8bddf2ba595347820ac387751ce to your computer and use it in GitHub Desktop.
Throttle with trailing call preservation
// 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;
}
};
}
@msssk
Copy link
Author

msssk commented Apr 15, 2016

Goal: a throttle method for rapid event handlers, such as mousemove and scroll.

Presumed desirable traits:

  1. Responsive: execute the first invocation without delay; execute other eligible invocations with minimal delay
  2. Accurate: ensure that trailing invocations during the timeout period are not discarded when the event ceases to be triggered
  3. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment