MCPcopy
hub / github.com/stemkoski/stemkoski.github.com / debounce

Function debounce

MathBox/mathbox-bundle.js:40508–40607  ·  view source on GitHub ↗

* Creates a function that will delay the execution of `func` until after * `wait` milliseconds have elapsed since the last time it was invoked. * Provide an options object to indicate that `func` should be invoked on * the leading and/or trailing edge of the `wait` timeout. Subsequent

(func, wait, options)

Source from the content-addressed store, hash-verified

40506 * }, false);
40507 */
40508 function debounce(func, wait, options) {
40509 var args,
40510 maxTimeoutId,
40511 result,
40512 stamp,
40513 thisArg,
40514 timeoutId,
40515 trailingCall,
40516 lastCalled = 0,
40517 maxWait = false,
40518 trailing = true;
40519
40520 if (!isFunction(func)) {
40521 throw new TypeError;
40522 }
40523 wait = nativeMax(0, wait) || 0;
40524 if (options === true) {
40525 var leading = true;
40526 trailing = false;
40527 } else if (isObject(options)) {
40528 leading = options.leading;
40529 maxWait = 'maxWait' in options && (nativeMax(wait, options.maxWait) || 0);
40530 trailing = 'trailing' in options ? options.trailing : trailing;
40531 }
40532 var delayed = function() {
40533 var remaining = wait - (now() - stamp);
40534 if (remaining <= 0) {
40535 if (maxTimeoutId) {
40536 clearTimeout(maxTimeoutId);
40537 }
40538 var isCalled = trailingCall;
40539 maxTimeoutId = timeoutId = trailingCall = undefined;
40540 if (isCalled) {
40541 lastCalled = now();
40542 result = func.apply(thisArg, args);
40543 if (!timeoutId && !maxTimeoutId) {
40544 args = thisArg = null;
40545 }
40546 }
40547 } else {
40548 timeoutId = setTimeout(delayed, remaining);
40549 }
40550 };
40551
40552 var maxDelayed = function() {
40553 if (timeoutId) {
40554 clearTimeout(timeoutId);
40555 }
40556 maxTimeoutId = timeoutId = trailingCall = undefined;
40557 if (trailing || (maxWait !== wait)) {
40558 lastCalled = now();
40559 result = func.apply(thisArg, args);
40560 if (!timeoutId && !maxTimeoutId) {
40561 args = thisArg = null;
40562 }
40563 }
40564 };
40565

Callers 1

throttleFunction · 0.85

Calls 2

isFunctionFunction · 0.70
isObjectFunction · 0.70

Tested by

no test coverage detected