requestAnimationFrame undefined in iOS UIWebView?

2019-02-18 07:11发布

问题:

requestAnimationFrame seems to be undefined in UIWebView. Is there another function that does the same thing or do I have to use setTimeout?

回答1:

It looks like this is not currently supported in all versions of WebKit, so you'll have to use a timeout. This site provides an example of how to create a cross-platform solution:

// via http://paulirish.com/2011/requestanimationframe-for-smart-animating/
window.requestAnimFrame = (function(){
    return  window.requestAnimationFrame   || 
        window.webkitRequestAnimationFrame || 
        window.mozRequestAnimationFrame    || 
        window.oRequestAnimationFrame      || 
        window.msRequestAnimationFrame     || 
        function(/* function */ callback, /* DOMElement */ element){
             window.setTimeout(callback, 1000 / 60);
        };
})();