r/coffeescript • u/nikesoccer01 • Oct 15 '17
Does anyone have a throttle function in coffeescript they can provide me?
I've been trying to find a throttle function to limit mousemove from being called a billion times and nothing I've found is working at all.
4
Upvotes
View all comments
Show parent comment
1
u/R1cket Oct 16 '17
I think you mean not written in coffeescript. Second this though.
Another similar function that's good to know is debounce. It will wait until some duration has passed since the function was last called, before calling the given function once. In the past I have used it for a search box that auto-searches; the onkeypress handler should be a debounce wrapper around the search function, with a time of maybe 1 or 2 seconds. That way it won't perform a search for every letter the user types, but only when the user pauses for that duration.
So if nikesoccer01 wants to wait til the user stops moving the mouse or til they pause for a moment, then they would use debounce.
Also sidenote for both of you; lodash is said to be better than underscore for various reasons but otherwise they have a pretty identical feature set and are almost fully compatible with each other. I don't know enough to be able to defend one or the other but I reach for lodash in my new projects.