Background:
When using setInterval or setTimeout we receive a single Id to cancel/clear the actions.
setInterval/setTimeout:
const timeoutId = setTimeout(() => {clearTimeout(timeoutId)}, 1000);
const intervalId = setInterval(() => {clearInterval(intervalId)}, 1000);
requestAnimationFrame: When using raf, for every iteration of step we reset the rafId and we only call cancelAnimationFrame on latest rafId?
let rafId;
function step(timestamp) {
rafId = requestAnimationFrame(step);
console.log(timestamp, rafId)
if(timestamp >= 1000) {
cancelAnimationFrame(rafId);
}
}
rafId = requestAnimationFrame(step);
Question:
How does the cancelAnimationFrame know to close out all previous calls to requestAnimationFrame when the rafId
is constantly being updated?
I have a guess that cancelAnimationFrame works off of a frameId and closes everything behind it.
If thats true, how does it work with multiple requestAnimationFrames
(as they will share a frameIds)