I am trying to migrate some old animations that are based on setInterval to requestAnimationFrame, however my updateState function requires some arguments and I can't understand how to pass them with requestAnimationFrame. Here's an example how the old code calls the drawing function:

var interval = setInterval(function(){
     oldValue < newValue ? updateState(oldValue += max/100) : clearInterval(interval);
},16);

All examples of rAF show it used like:

function updateState() {
    requestAnimFrame( updateState );
}

updateState();

How can I pass in my arguments to the updateState function?

有帮助吗?

解决方案

You can do the same you do with setInterval, put your call to updateState in a closure.

function updateState() {
    requestAnimFrame( function(){
        if( oldValue < newValue ) {
            updateState(oldValue += max/100);
        }
    });

    // The rest of your code
}

updateState();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top