First, SetTimeout doesn't take into account what's still going on in the browser. The page may, for example, be in an inactive browser deposit. In doing so, it will use the resources of the processor, regardless.By the way, smart Chrome makes setInterval and setTimeout equal 1fps in hidden deposits... but as far as I know, it only makes chrome :Second, SetTimeout requires a page rewriting not at the same time as your computer does (and he does it regularly). It means that your poor browser has to synchronize your grief ensemble with the updating of the whole screen, and if its frequency is not synchronized with the updating of the whole screen, it may require more computing power. And it's the processor's download, heating, the sound of the killer, the flow of the mobile mottos... etc.Another aspect is the animation of several elements at once. Of course, we can try to synchronize this, but... it's another nightmare, in the case of a multi-dimensional animation that occurs one time.To overcome these problems, Mozilla (Firefox) proposed the function requestAnimationFrame, which was subsequently adopted and improved by the WebKit (Chrome and Safari). It provides a built-in API for launching any type of animation in the browser (DOM elements, canvas, WebGL, etc.)function step() {
requestAnimationFrame(step);
// описываем один шаганимации тут
}
step();
That's awesome! It's like setTimeout, as described above, but with requestAnimationFrame instead of him! If you wish, you can pass another parameter - animated element: requestAnimationFrame(step, element);However, as you may have noticed, we did not specify the interval. How often does our function be called? It depends on the frequency of your browser and computer staff (usually 60 personnel per second). The key difference is that you ask the browser to perform the function (in our example of step) at the earliest opportunity rather than at the specified interval. Another advantage of this approach is that browsers may reduce requestAnimationFrame depending on the load, visibility of the cell and state of the battery.A few more valuable virtues, please AnimationFrame, that he will group all your animations in one browser repaint. It'll save the processor's resources and let your device be faster and live longer.If you use the requestAnimationFrame, all your animation will be smooth and beautiful, synchronized with your graphic processor (GPU) and eating much less central processor resources (CPU).Source http://html5.by/blog/what-is-requestanimationframe/