MCPcopy
hub / github.com/lodash/lodash / debounce

Function debounce

lodash.js:10403–10526  ·  view source on GitHub ↗

* Creates a debounced function that delays invoking `func` until after `wait` * milliseconds have elapsed since the last time the debounced function was * invoked. The debounced function comes with a `cancel` method to cancel * delayed `func` invocations and a `flush` method to immedi

(func, wait, options)

Source from the content-addressed store, hash-verified

10401 * jQuery(window).on('popstate', debounced.cancel);
10402 */
10403 function debounce(func, wait, options) {
10404 var lastArgs,
10405 lastThis,
10406 maxWait,
10407 result,
10408 timerId,
10409 lastCallTime,
10410 lastInvokeTime = 0,
10411 leading = false,
10412 maxing = false,
10413 trailing = true;
10414
10415 if (typeof func != 'function') {
10416 throw new TypeError(FUNC_ERROR_TEXT);
10417 }
10418 wait = toNumber(wait) || 0;
10419 if (isObject(options)) {
10420 leading = !!options.leading;
10421 maxing = 'maxWait' in options;
10422 maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait) : maxWait;
10423 trailing = 'trailing' in options ? !!options.trailing : trailing;
10424 }
10425
10426 function invokeFunc(time) {
10427 var args = lastArgs,
10428 thisArg = lastThis;
10429
10430 lastArgs = lastThis = undefined;
10431 lastInvokeTime = time;
10432 result = func.apply(thisArg, args);
10433 return result;
10434 }
10435
10436 function leadingEdge(time) {
10437 // Reset any `maxWait` timer.
10438 lastInvokeTime = time;
10439 // Start the timer for the trailing edge.
10440 timerId = setTimeout(timerExpired, wait);
10441 // Invoke the leading edge.
10442 return leading ? invokeFunc(time) : result;
10443 }
10444
10445 function remainingWait(time) {
10446 var timeSinceLastCall = time - lastCallTime,
10447 timeSinceLastInvoke = time - lastInvokeTime,
10448 timeWaiting = wait - timeSinceLastCall;
10449
10450 return maxing
10451 ? nativeMin(timeWaiting, maxWait - timeSinceLastInvoke)
10452 : timeWaiting;
10453 }
10454
10455 function shouldInvoke(time) {
10456 var timeSinceLastCall = time - lastCallTime,
10457 timeSinceLastInvoke = time - lastInvokeTime;
10458
10459 // Either this is the first call, activity has stopped and we're at the
10460 // trailing edge, the system time has gone backwards and we're treating

Callers 1

throttleFunction · 0.85

Calls 2

toNumberFunction · 0.85
isObjectFunction · 0.85

Tested by

no test coverage detected