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

help: Confused with 'requestIdleCallback(workLoop)' #43

Open
KelvinQiu802 opened this issue Aug 18, 2022 · 2 comments
Open

help: Confused with 'requestIdleCallback(workLoop)' #43

KelvinQiu802 opened this issue Aug 18, 2022 · 2 comments

Comments

@KelvinQiu802
Copy link

In Step III: Concurrent Mode, I learn that: requestIdleCallback(func) will call the call back function when the browser is idle.
But in workLoop function, that while loop confused me.

function workLoop(deadline) {
  let shouldYield = false
  while (nextUnitOfWork && !shouldYield) {
    nextUnitOfWork = performUnitOfWork(
      nextUnitOfWork
    )
    shouldYield = deadline.timeRemaining() < 1
  }
  requestIdleCallback(workLoop)
}

My question is: if shoudYield is false, it means that the browser is busy, so we should stop calling the next unit of work.
But in this while loop, if shoudYield is false, the loop will continue and do the next unit of work. Why?

@pomber
Copy link
Owner

pomber commented Aug 18, 2022

if shoudYield is false, it means that the browser is busy, so we should stop calling the next unit of work

It's the opposite. shouldYield means "should stop". If shouldYield is false it means we should continue performing units of work.

@KelvinQiu802
Copy link
Author

HaHa~, it's my mistake!
Here is the doc about the return value.

Returns a DOMHighResTimeStamp, which is a floating-point value providing an estimate of the number of milliseconds remaining in the current idle period.

So deadline.timeRemaining() < 1 means that there is no remaining time to do the work, we should break the while loop and request another callback.

Thank you! Love your amazing project, I will continue learning with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants