The lodash _.debounce method for delay of function invoking.
1 - The Lodash debounce method and what to know first
1.3 - Basic example of _.debounce
For this basic debounce method example I just called it and pass the function that I want debounced, and a time in milliseconds as a second argument. Once that is done a debounced function will then be returned, once called the function will be invoked once the given about of time passes.
1.2 - Making a loop
1.3 - flushing
A _.debounce method comes with a flush method that can be used to call the method at once right alway. This flush method can be called off from and object that is returned when calling lodash denounce.
2.1 - Basic setTimeout example
So then for a basic setTimeout method example I just call the setTimeout method and pass the function that I want called after a time out value in milliseconds. That is it then as the return value is not a function but a number that is used as an id for this that can be used to cancel the calling of the function later on with a method known as clearTimeout.
2.2 - Basic app loop example
I can then make a loop example of the setTimeout method in a similar way of that of the loop example using lodash denounce. The difference is that I pass a reference to the function that I pass to setTeout rather than calling the function that is returned because once again it is not a function that is returned by setTimeout.
2.3 - The setInterval method
The setInterval method is another option that can be used to delay the calling of a function, but it will not just delay the function, but rather call it over and over again.
3 - Nodejs and delaying a function
Now for at least one example of delaying a function in a nodejs environment.
3.1 - The argv array in the process global
Here I have an example where I am using the process global in nodejs to get argument values to set the delay, as well as the number of times to call a function.
4.1 - Request Animation Frame
5 - Conclusion
The _.debounce method in lodash can be useful when making some methods that need to do something every once in a while, but also need to be check right away in some situations. So then the debounce method is very similar to that of native methods like setTimeout only with a few key differences such as the fact that the return value is a function rather than an id that can be used with clearTimeout, and that the delay will start when the returned function is called rater than right away.