The lodash _.throttle method.
1 - The Basics of lodash throttle and what else to know first
The source code examples in this post are on Githun
In my test lodash repository I have the source code examples for this post. This is also where I am parking the source code examples for my other posts on lodash as well.
1.1 - A lodash _.throttle basic example
So here is a quick basic example of the lodash throttle method in action. I am using the lodash throttle method by passing a function as the first argument that is to be called each time a certain about of time has passed. After the function as the first argument is passed I then pass a number value that is the amount of time to let pass between times that the function is called as the second argument. What is then returned is a throttled method that will only file once that amount of time has passed.
_.throttle differers from setTimeout and setInterval as it returns a new function that will only fire once the amount of time has passed when it is being called, rather than setting a function to call after an amount of time has passed, or at a certain interval.
1.2 - App loop example
One of the main use case examples for this kind of function would be to create a function that will be called over an over again in a main app loop method and I want to make it so that the function will only fire at a certain rate no mater the rate at which the main app loop method is firing. So then with this example I have a very crude form of a state machine where there is a starting state and then a main game state.
2 - Vanilla js lodash throttle alternative examples
2.1 - Basic Lodash throttle clone Using closures.
So I started out with writing a function expression, and then just have it so that function expression returns another function expression. Inside the body of the outer function I have a variable that will store the amount of time that has elapsed sense the function was fist created, or sense the last time the function that is passed as an argument is called. Inside the body of the inner function I am testing if the amount of time that has elapsed is greater than the set rate, if so I am calling the given method, and setting the last time variable to the current time.
So something like this:
And it works more or less the same way as lodash throttle.
So if you are new to writing closures writing a lodash throttle clone is a good starting point. When it comes to writing a clone of this kind of method there is making it so that it works more or less the same way, and with the same set of features, but there is also adding additional features to make it a custom trailered kind of throttle method.
2.2 - A not so basic lodash throttle clone
So I made a more complex version of this lodash throttle clone just for the sake of writing about some additional talking points as to why it might not be such a bad idea to take the time to write a custom method for this sort of thing. In this lodash throttle clone I am still returning a function, but I am also appending some additional methods to the function object for calling the given method right away, and also for ajusting the rate.
I then made an example using it where the rate at which the function is called goes up and down.
Maybe this is not a practical use case example, but if I has a more clear idea of an actual project that would make use of a method like lodash throttle then I might want to add some custom functionality to it. Or fine and alternative way all together for this sort of thing actually. The lodash throttle method is no replacement for state machines, or any kind of main app loop that might need to be called at a certain fixed rate.
3 - Conclusion
So then when it comes to additional examples of this sort of thing there is maybe looking into one or more canvas examples which can prove to be a fun way of learning about the native method alternatives to the lodash throttle method. I have canvas example that is an example of a basic state machine that uses request animation frame and canvas elements when it comes to getting into making state machines.