const prev = performance.now();
requestAnimationFrame(now => console.log(now - prev));
Why now
is always less than prev
(at least in the last Chrome branches)?
Screenshot:
const prev = performance.now();
requestAnimationFrame(now => console.log(now - prev));
Why now
is always less than prev
(at least in the last Chrome branches)?
Screenshot:
This happens because you request an animation between current frame and the next repaint but you could actually have missed the first callback trigger of the current frame which is when the timestamp
argument was established.
Many calls to requestAnimationFrame
callback could occur in this timespan, but all of them will have the timestamp
of the first requestsAnimationFrame
's callback trigger in the active frame which may have occurred before your call to performance.now()
.
MDN also states:
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.