When using throttle (or debounce), there is no simple way to throttle in such a way that the function ignores calls during the delay period. Rather, throttle will delay execution but guarantee that it occurs. Consider the use case of renewing authentication tokens as the user takes actions on a rich page. If we wanted a large delay for this purpose, say, five minutes, we might want to do something like this:
However, if the user takes two sequential actions and then sites idle for four and a half minutes, we'll get this behavior:
1) An immediate renewal (desired)
2) A renewal five minutes later, even though the user hasn't acted recently
What I really want is to be able to say "run this function only when it is called and not in throttled mode". This was proposed in #251, but shot down after a decision was made in #170. However, as @yuchi points out, that decision was later reversed. @acidron also commented that he was looking for this behavior in that closed pull request.
Sounds fine -- feel free to send a pull request that adds a third argument to throttle, similar to the current third argument to debounce.
Add third flag to throttle resembling debounce
I believe the patch at #1019 doesn't address this issue. The patch at #1019 addresses optionally executing immediately but not the trailing call.
Here are the differences in the two functions:
It would be wonky to change the immediate flag for throttle to be consistant with which edge of the function queuing for subsequent calls occurs on. If you made the immediate flag for throttle and debounce behave the same way, the default behavior of one of them would have to change.
Its a half baked solution, but for example, if the immediate flag was ternary it could be consistant.
1) trigger leading
2) trigger both
3) trigger falling
This would change that same venn diagram to be:
Problem is there does not seem to be a way to make it not confusing.
Fixes #926. Throttle now takes 'leading' and 'trailing' options to cu…