Home  >  Q&A  >  body text

javascript - rquestAnimationFrame()做出setTimeout()效果

在做一个小游戏的时候运用了JQ动画,并且用了setTimeout()来设置动画延迟播放,在PC上游戏完美运行,可是在手机真机上测试时发现游戏动画部分好卡顿,后来发现是setTimeout()造成的,想用requestAnimationFrame()来代替setTimeout(),查找了好多文章都看不懂,主要是百度到的相关文章好少,谷歌到的又大部分看不明,求大神们帮帮忙,谢谢。

伊谢尔伦伊谢尔伦2717 days ago691

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-10 17:40:21

    比如说我不停地重复输出1,使用setTimeout代码如下:

    setTimeout(animate,100)
        function animate(){
            console.log(1);
            setTimeout(animate,100);
        }

    而使用requestAnimationFrame代码如下

        requestAnimationFrame(animate);
        function animate(){
            console.log(1);
            requestAnimationFrame(animate);
        }

    是不是非常地简单?

    reply
    0
  • Cancelreply