| 9215 | } |
| 9216 | |
| 9217 | function debounced() { |
| 9218 | args = arguments; |
| 9219 | stamp = now(); |
| 9220 | thisArg = this; |
| 9221 | trailingCall = trailing && (timeoutId || !leading); |
| 9222 | |
| 9223 | if (maxWait === false) { |
| 9224 | var leadingCall = leading && !timeoutId; |
| 9225 | } else { |
| 9226 | if (!maxTimeoutId && !leading) { |
| 9227 | lastCalled = stamp; |
| 9228 | } |
| 9229 | var remaining = maxWait - (stamp - lastCalled), |
| 9230 | isCalled = remaining <= 0 || remaining > maxWait; |
| 9231 | |
| 9232 | if (isCalled) { |
| 9233 | if (maxTimeoutId) { |
| 9234 | maxTimeoutId = clearTimeout(maxTimeoutId); |
| 9235 | } |
| 9236 | lastCalled = stamp; |
| 9237 | result = func.apply(thisArg, args); |
| 9238 | } |
| 9239 | else if (!maxTimeoutId) { |
| 9240 | maxTimeoutId = setTimeout(maxDelayed, remaining); |
| 9241 | } |
| 9242 | } |
| 9243 | if (isCalled && timeoutId) { |
| 9244 | timeoutId = clearTimeout(timeoutId); |
| 9245 | } |
| 9246 | else if (!timeoutId && wait !== maxWait) { |
| 9247 | timeoutId = setTimeout(delayed, wait); |
| 9248 | } |
| 9249 | if (leadingCall) { |
| 9250 | isCalled = true; |
| 9251 | result = func.apply(thisArg, args); |
| 9252 | } |
| 9253 | if (isCalled && !timeoutId && !maxTimeoutId) { |
| 9254 | args = thisArg = null; |
| 9255 | } |
| 9256 | return result; |
| 9257 | } |
| 9258 | debounced.cancel = cancel; |
| 9259 | return debounced; |
| 9260 | } |