简体   繁体   中英

Migration from setInterval to requestAnimationFrame

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM