Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: set timerId at startTimer #5766

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ function debounce(func, wait, options) {
function startTimer(pendingFunc, milliseconds) {
if (useRAF) {
root.cancelAnimationFrame(timerId);
return root.requestAnimationFrame(pendingFunc);
timerId = root.requestAnimationFrame(pendingFunc);
}
// eslint-disable-next-line @typescript-eslint/no-implied-eval
return setTimeout(pendingFunc, milliseconds);
timerId = setTimeout(pendingFunc, milliseconds);
}

function cancelTimer(id) {
Expand All @@ -119,7 +119,7 @@ function debounce(func, wait, options) {
// Reset any `maxWait` timer.
lastInvokeTime = time;
// Start the timer for the trailing edge.
timerId = startTimer(timerExpired, wait);
startTimer(timerExpired, wait);
// Invoke the leading edge.
return leading ? invokeFunc(time) : result;
}
Expand Down Expand Up @@ -153,7 +153,7 @@ function debounce(func, wait, options) {
return trailingEdge(time);
}
// Restart the timer.
timerId = startTimer(timerExpired, remainingWait(time));
startTimer(timerExpired, remainingWait(time));
return undefined;
}

Expand Down Expand Up @@ -199,12 +199,12 @@ function debounce(func, wait, options) {
}
if (maxing) {
// Handle invocations in a tight loop.
timerId = startTimer(timerExpired, wait);
startTimer(timerExpired, wait);
return invokeFunc(lastCallTime);
}
}
if (timerId === undefined) {
timerId = startTimer(timerExpired, wait);
startTimer(timerExpired, wait);
}
return result;
}
Expand Down