1/**� 2 * DEBOUNCE� 3 * Instead of calling the function immediately, wait at least `delay` ms before calling it.� 4 * Example: debounce(function(), 200) will only call the function after a 200ms pause.� 5 * Useful, for example, to check an available username (wait to pause typing and check).� 6 */� 7if ( typeof debounce !== 'function' ) {� 8 function debounce( func, wait, options ) {� 9 let lastArgs,� 10 lastThis,� 11 maxWait,� 12 result,� 13 timerId,� 14 lastCallTime,� 15 lastInvokeTime = 0,� 16 leading = false,� 17 maxing = false,� 18 trailing = true;� 19 if ( typeof func !== 'function' ) {� 20 throw new TypeError( FUNC_ERROR_TEXT );� 21 }� 22 wait = Number( wait ) || 0;� 23 if ( typeof options === 'object' ) {� 24 leading = !!options.leading;� 25 maxing = 'maxWait' in options;� 26 maxWait = maxing ? Math.max( Number( options.maxWait ) || 0, wait ) : maxWait;� 27 trailing = 'trailing' in options ? !!options.trailing : trailing;� 28 }� 29� 30 function invokeFunc( time ) {� 31 let args = lastArgs,� 32 thisArg = lastThis;� 33� 34 lastArgs = lastThis = undefined;� 35 lastInvokeTime = time;� 36 result = func.apply( thisArg, args );� 37 return result;� 38 }� 39� 40 function leadingEdge( time ) {� 41 // Reset any `maxWait` timer.� 42 lastInvokeTime = time;� 43 // Start the timer for the trailing edge.� 44 timerId = setTimeout( timerExpired, wait );� 45 // Invoke the leading edge.� 46 return leading� 47 ? invokeFunc( time )� 48 : result;� 49 }� 50� 51 function remainingWait( time ) {� 52 let timeSinceLastCall = time - lastCallTime,� 53 timeSinceLastInvoke = time - lastInvokeTime,� 54 result = wait - timeSinceLastCall;� 55 console.log( 'remainingWait' );� 56 return maxing ? Math.min( result, maxWait - timeSinceLastInvoke ) : result;� 57 }� 58� 59 function shouldInvoke( time ) {� 60 let timeSinceLastCall = time - lastCallTime,� 61 timeSinceLastInvoke = time - lastInvokeTime;� 62 // Either this is the first call, activity has stopped and we're at the trailing� 63 // edge, the system time has gone backwards and we're treating it as the� 64 // trailing edge, or we've hit the `maxWait` limit.� 65 return ( lastCallTime === undefined || ( timeSinceLastCall >= wait ) || ( timeSinceLastCall < 0 ) || ( maxing && timeSinceLastInvoke >= maxWait ) );� 66 }� 67� 68 function timerExpired() {� 69 const time = Date.now();� 70 if ( shouldInvoke( time ) ) {� 71 return trailingEdge( time );� 72 }� 73 // Restart the timer.� 74 timerId = setTimeout( timerExpired, remainingWait( time ) );� 75 }� 76� 77 function trailingEdge( time ) {� 78 timerId = undefined;� 79� 80 // Only invoke if we have `lastArgs` which means `func` has been debounced at� 81 // least once.� 82 if ( trailing && lastArgs ) {� 83 return invokeFunc( time );� 84 }� 85 lastArgs = lastThis = undefined;� 86 return result;� 87 }� 88� 89 function cancel() {� 90 if ( timerId !== undefined ) {� 91 clearTimeout( timerId );� 92 }� 93 lastInvokeTime = 0;� 94 lastArgs = lastCallTime = lastThis = timerId = undefined;� 95 }� 96� 97 function flush() {� 98 return timerId === undefined ? result : trailingEdge( Date.now() );� 99 }� 100� 101 function debounced() {� 102 let time = Date.now(),� 103 isInvoking = shouldInvoke( time );� 104 lastArgs = arguments;� 105 lastThis = this;� 106 lastCallTime = time;� 107� 108 if ( isInvoking ) {� 109 if ( timerId === undefined ) {� 110 return leadingEdge( lastCallTime );� 111 }� 112 if ( maxing ) {� 113 // Handle invocations in a tight loop.� 114 timerId = setTimeout( timerExpired, wait );� 115 return invokeFunc( lastCallTime );� 116 }� 117 }� 118 if ( timerId === undefined ) {� 119 timerId = setTimeout( timerExpired, wait );� 120 }� 121 return result;� 122 }� 123� 124 debounced.cancel = cancel;� 125 debounced.flush = flush;� 126 return debounced;� 127 }� 128}� 129� 130/**� 131 * THROTTLE� 132 * Run the function as soon as it's called, but prevent further calls during `delay` ms� 133 * Example: throttle(function(), 200) will only run function() once every 200 ms.� 134 * Useful, for example, to avoid constant processing while typing in a live search box.� 135 */� 136if ( typeof throttle !== 'function' ) {� 137 function throttle( func, wait, options ) {� 138 let leading = true,� 139 trailing = true;� 140� 141 if ( typeof func !== 'function' ) {� 142 throw new TypeError( FUNC_ERROR_TEXT );� 143 }� 144 if ( typeof options === 'object' ) {� 145 leading = 'leading' in options ? !!options.leading : leading;� 146 trailing = 'trailing' in options ? !!options.trailing : trailing;� 147 }� 148 return debounce( func, wait, {� 149 leading,� 150 maxWait: wait,� 151 trailing,� 152 } );� 153 }� 154}�
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.