Take this code:
requestAnimationFrame(function (timestamp) {
console.log('one', timestamp);
});
requestAnimationFrame(function (timestamp) {
console.log('two', timestamp);
});
// logs:
// "one", 184.6999999834225
// "two", 184.6999999834225
The timestamp is milliseconds since the page loaded. Note these two rAF calls return different IDs which you can cancel individually.
Now let's make the first callback do something very expensive:
requestAnimationFrame(function (timestamp) {
console.log('one', timestamp);
// block for 1 second
const endAt = Date.now() + 1000;
while (true) {
if (Date.now() >= endAt) break;
}
});
requestAnimationFrame(function (timestamp) {
console.log('two', timestamp);
});
// LOGS:
// "one", 189.32800000533462
// "two", 189.32800000533462 (this appears one second later)
I'm confused: the second one runs a whole second later, but gets the same timestamp. Why can't it get a new timestamp, one corresponding with whatever the current monitor refresh frame is now at the time it's being called?
If rAF decides ahead of time that your callback must be run in the same frame as another callback, regardless of how long that other callback might take, then the 'frame' seems like a meaningless concept that doesn't correspond with a single monitor refresh - so what's the point?
I'm sure there's a good reason why it's implemented this way, I just want to understand it.