3
I am creating an application where I need to check if an X element was created on the page and thus take some actions. To make that check, I’m using the setInterval
.
A few minutes ago, I remembered there’s a function requestAnimationFrame
, commonly used to create games. I also remembered that this function is much faster, or rather more "fluid" apparently than the setInterval
.
So I kept thinking. Which of these two functions is the best? What are the disadvantages and advantages of each of them?
Why don’t we just use requestAnimationFrame
when we don’t need to define a waiting time since it has apparently a better performance? Also, if this function is really better, why not create a parameter to spend a waiting time and so replace the function setInterval
?
Now back to the main question: what is the difference between the function requestAnimationFrame
and the function setInterval
in an in-depth way?
Another fundamental difference is that
requestAnimationFrame
does not execute callback if the active browser tab is not that of the application or the browser is minimized– Costamilam