In a nutshell
This is the method signature:
_.debounce(func, [wait=0], [options={}])
Lodash’s debounce
function creates a “debounced” function that delays invoking the given func
until after wait
milliseconds have elapsed since the last invocation.
Examples
// Avoid costly calculations while the window size is in flux.
jQuery(window).on('resize', _.debounce(calculateLayout, 150));
// Cancel the trailing debounced invocation.
jQuery(window).on('popstate', debounced.cancel);
Other notes
- You can use a
cancel
method to cancel delayedfunc
invocations. - You can use a
flush
method to immediately invoke delayedfunc
invocations. - The
func
will be invoked with the last arguments passed to debounced function. - Subsequent calls of
func
will return the result of the last invocation.