Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: add features from Ben Alman's throttle/debounce plugin #91

Closed
timmolendijk opened this issue Jan 6, 2011 · 14 comments
Closed

Comments

@timmolendijk
Copy link

See http://benalman.com/projects/jquery-throttle-debounce-plugin/

The no_trailing and at_begin parameters are extremely useful and I'd love to see them added to Underscore's throttle and debounce functions.

@jashkenas
Copy link
Owner

Perhaps -- can you share a real world example where they come in handy? I can't think of any off the top of my head...

@timmolendijk
Copy link
Author

Well, throttling and debouncing are quite abstract concepts of course, so there are countless scenarios. I believe the parameters are not a luxury as they enable use cases that are otherwise simply not possible. I can give you an example from my own work, although I'm not sure if it's really helpful:

I'm using debouncing to make sure that AJAX requests are not being issued more than four times per second, like so (Alman's API): $.debounce(250, true, issue_ajax_request). This is useful to prevent superfluous requests to be sent to the server. When a request is being sent, it ignores any requests that are being issued in the next 250 ms. But sometimes this delay after the request is not really what you want, and instead you want a delay before the request is being sent. For example when various parameters are being changed at the same time, all of which lead to an AJAX request being issued. What you really want is to send out one request though. This request should combine all the parameter changes that were happening at more or less the same moment in time. So in this case what you want to do is: $.debounce(250, false, issue_ajax_request). It really comes down to which function call you want to use and which ones you want to discard; discard until discard is no longer an option, or use as soon as it no longer must be discarded.

Alas, not sure if this makes sense to an outsider. I could also bounce the question; if you can tell how to achieve the same effect (of no_trailing and at_begin) without having these parameters, this feature request would go away as far as I'm concerned.

@cowboy
Copy link

cowboy commented Jan 9, 2011

If you end up wanting this in Underscore, I'd be happy to contribute the code, just let me know.

@jashkenas
Copy link
Owner

cowboy: Thanks for the kind offer.

timmolendijk: I just mostly want to wrap my head around the use cases first... Let me try to play Devil's advocate:

You don't need at_begin for debounce, because you don't have the data yet at the beginning of the sequence -- the user is still twiddling the parameter, and it's when they stop twiddling it, and you actually have a value, that you want to save the data. I can't really imagine a situation where you'd want to ignore all further changes to the value.

You don't need no_trailing for the same reason -- the data is guaranteed to have changed since the last time your function was invoked, and you need the trailing invocation to ensure that your state is correct.

@cowboy
Copy link

cowboy commented Jan 9, 2011

While debouncing is very commonly used for rate-limiting AJAX requests resulting from text entry, it can be used in other ways. For example, imagine that you have a game where responsiveness is critical, but the gamer must be prohibited from triggering an action more than once, until some kind of inactivity threshold has been met. Using the at_begin, flag, this behavior could be easily enabled.

The no_trailing flag can be utilized in the exact same way, where you're trying to throttle an action that is not tightly coupled to the changing of data, but is itself just an action to be throttled. When the gamer stops pressing the "shoot" button, if the throttled function is between-executions, it shouldn't execute one more time after they've stopped spamming the button "for good measure," it should just stop.

I'm not suggesting in any way that Underscore should attempt to handle these scenarios, just that they are possibly valid for users of a generalized Throttle/Debounce library (which is what my plugin tries to be).

@timmolendijk
Copy link
Author

While in the case of rate-limiting AJAX requests resulting from text entry, what you want is at_begin = false, in other cases of rate-limiting AJAX requests you do want at_begin = true. There are many scenarios in which there is no reason to have a delay before issuing the first request. For example when the request is issued as result of a button click. Here you simply want maximum responsiveness and therefore a request should be issued as soon as a click is detected. Yet, you still want to do rate-limiting, as an accidental double click should not result in two consecutive requests. Of course there are other ways of filtering out superfluous clicks, but implementing it as a debounce on the request with at_begin = true is a very nice generic solution that complements the alternative debouncing scenario (in which at_begin = false).

@alanhogan
Copy link
Contributor

It seems these got included into Underscore — should this issue be closed?

@timmolendijk
Copy link
Author

Nice! Hadn't noticed yet. Yeah I think this resolves my issue. Thx

@elias6
Copy link
Contributor

elias6 commented Mar 29, 2011

I am looking at the definitions for throttle and debounce in the source for the development version of Underscore 1.1.5 and I don't see the features. Am I looking in the wrong place?

@alanhogan
Copy link
Contributor

I was looking e.g. http://documentcloud.github.com/underscore/#throttle

@elias6
Copy link
Contributor

elias6 commented Mar 30, 2011

I see the descriptions of throttle and debounce, but I don't see any mention of no_trailing and at_begin.

@timmolendijk
Copy link
Author

@mikez302: _.debounce(func, 250) translates to Alman's $.debounce(250[, false], func), and $.debounce(250, true, func) can be reconstructed using _.once.

@elias6
Copy link
Contributor

elias6 commented Apr 13, 2011

What about throttle and no_trailing?

Also, just because it is possible to implement these features myself, is that a reason not to include them in underscore? In theory, I can implement all the features in underscore myself.

@dev-bjoern
Copy link

i was just looking for the no_trailing option for throttle. but its not there.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants