Why does if condition executes in this requestAnimationFrame code?

requestAnimationFrame takes a callback function. How/when it is called, depends on the API of the higher-order function (i.e. the function/method that takes the callback).

In the case of requestAnimationFrame I believe the “event” that drives it is timing based.

Window: requestAnimationFrame() method - Web APIs | MDN

callback

The function to call when it’s time to update your animation for the next repaint. The callback function is passed one single argument, a DOMHighResTimeStamp similar to the one returned by performance.now(), indicating the point in time when requestAnimationFrame() starts to execute callback functions.


For a more broad look into scheduling/queuing by the engine/runtime.