Suppose I have several animations (functions that change something), In general, I can simply write requestAnimationFrame(functionName) in each of them, run them when the page loads, thus it turns out that requestAnimationFrame is called many times and I don’t know well or bad (I worked with setInterval and there it is naturally bad). Or I can make a function like this:

 on.frame = (function () { var fns = []; function frame () { var now = new Date(); for ( let i = 0; i < fns.length; i++ ) fns[i](now); if (fns.length != 0) requestAnimationFrame(frame); } return function (fn) { if(fns.length == 0){ fns.push(fn); requestAnimationFrame(frame); } else fns.push(fn); } })(); 

The point is to create an array with animations and execute them all in one function that is thrust into one requestAnimationFrame . Is it worth doing? After all, as far as I understand, requestAnimationFrame collects everything from the beginning that should change and then executes together.

  • Are all the animations endless? - Grundy
  • There may be endless, there may be for some period, any - Kirpich643

1 answer 1

It will work this way and that, but it is more logical to collect everything related to requestAnimationFrame - in one place.

  • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky