Skip to content

Can the check inside throttle's setTimeout() be omitted? #2022

Closed Answered by Grenish
HiChen404 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi there! HiChen404

if (Date.now() - lastTime >= wait) {
  fn.apply(context, args);
  lastTime = Date.now();
}

This condition checks whether enough time has passed since the last execution of the throttled function. If the time elapsed is greater than or equal to the specified wait time, the function is executed again.

The purpose of this condition is to handle cases where there's a delay between the setTimeout being triggered and the actual execution of the function. If the delay is longer than the specified wait time, the function should be executed immediately without waiting for the remaining time.

The purpose of the condition outside the setTimeout block is to handle cases where ther…

Replies: 9 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Chalarangelo
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
js Related to JavaScript content.
8 participants
Converted from issue

This discussion was converted from issue #1965 on September 27, 2023 17:06.