-1

Does the window.requestAnimationFrame function create new threads to let the function where it's called and the function itself run parallel? Because I don't understand why the following code, doesn't result in a stack overflow.

window.requestAnimationFrame calls main and main calls window.requestAnimationFrame and so on ...

w.rAF -> main -> w.rAF -> main -> w.rAF . . .

main.js

function main(currentTime){
    window.requestAnimationFrame(main);
    console.log(currentTime);
}

window.requestAnimationFrame(main);

index.html

<script src="main.js"></script>
tonik
  • 465
  • 1
  • 4
  • 15
  • 1
    It's almost the same as if you used `setTimeout`. It's just a different queue. – marzelin Sep 01 '20 at 13:02
  • Thank you, I understand it now. There was already a similar question online. Its just an async function call, im quite familiar with that because of flutter (dart). – tonik Sep 01 '20 at 13:44

2 Answers2

1

No. Functions passed to requestAnimationFrame are queued, not threaded.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

requestAnimationFrame tells the browser to run the function before the next frame is rendered. If you again call requestAnimationFrame in main it will put that function on the queue for the next (not the current) animationFrame.

In Javascript only workers are threaded.

requestAnimationFrame is a special function to execute code at the specific point in the Event Loop. I found this video quite helpful in explaining how the Event Loop works: https://www.youtube.com/watch?v=cCOL7MC4Pl0

Snuffels11
  • 56
  • 3