创建一个去抖动函数,该函数会延迟调用func,直到自上次调用去抖函数后等待几毫秒后。 去抖动函数带有一个取消方法来取消延迟的func调用和一个flush方法来立即调用它们。 提供选项以指示是否应在等待超时的前沿和/或后沿调用func。 func被提供给去抖动函数的最后一个参数调用。 随后调用debounced函数返回最后一次func调用的结果。
注意:如果前导和尾随选项为true,则只有在等待超时期间多次调用debounced函数时,才会在超时的后沿调用func。
如果wait为0且leading为false,则func调用被推迟到下一个tick,类似于超时值为0的setTimeout。
0.1.0
func
(功能):去抖功能。 [wait=0]
(数字):延迟的毫秒数。 [options={}]
(对象):选项对象。 [options.leading=false]
(boolean):指定在超时的前沿调用。 [options.maxWait]
(数字):最大时间func
可以在被调用之前被延迟。 [options.trailing=true]
(布尔值):指定在超时的后沿调用。 (功能):返回新的去抖功能。
// Avoid costly calculations while the window size is in flux. jQuery(window).on('resize', _.debounce(calculateLayout, 150)); // Invoke `sendMail` when clicked, debouncing subsequent calls. jQuery(element).on('click', _.debounce(sendMail, 300, { 'leading': true, 'trailing': false })); // Ensure `batchLog` is invoked once after 1 second of debounced calls. var debounced = _.debounce(batchLog, 250, { 'maxWait': 1000 }); var source = new EventSource('/stream'); jQuery(source).on('message', debounced); // Cancel the trailing debounced invocation. jQuery(window).on('popstate', debounced.cancel);Try in REPL