functioncancelTimer(id) { if (useRAF) { return root.cancelAnimationFrame(id) } clearTimeout(id) }
functionleadingEdge(time) { // Reset any `maxWait` timer. lastInvokeTime = time // Start the timer for the trailing edge. timerId = startTimer(timerExpired, wait) // Invoke the leading edge. return leading ? invokeFunc(time) : result }
functionremainingWait(time) { const timeSinceLastCall = time - lastCallTime const timeSinceLastInvoke = time - lastInvokeTime const timeWaiting = wait - timeSinceLastCall
functionshouldInvoke(time) { const timeSinceLastCall = time - lastCallTime const timeSinceLastInvoke = time - lastInvokeTime
// Either this is the first call, activity has stopped and we're at the // trailing edge, the system time has gone backwards and we're treating // it as the trailing edge, or we've hit the `maxWait` limit. return (lastCallTime === undefined || (timeSinceLastCall >= wait) || (timeSinceLastCall < 0) || (maxing && timeSinceLastInvoke >= maxWait)) }
functiontimerExpired() { const time = Date.now() if (shouldInvoke(time)) { returntrailingEdge(time) } // Restart the timer. timerId = startTimer(timerExpired, remainingWait(time)) }
functiontrailingEdge(time) { timerId = undefined
// Only invoke if we have `lastArgs` which means `func` has been // debounced at least once. if (trailing && lastArgs) { returninvokeFunc(time) } lastArgs = lastThis = undefined return result }