简体   繁体   English

setInterval和requestanimationframe有什么区别?

[英]what are the differences between setInterval and requestanimationframe?

I need your help , I am confused a little . 我需要你的帮助,我有点困惑。

my question is what are the differences between setInterval and requestanimationframe in JavaScript and when and where to use every one ? 我的问题是JavaScript中setIntervalrequestanimationframe之间的区别是什么,以及何时何地使用每一个?

The requestAnimationFrame method is used to sync animations with the screen updates. requestAnimationFrame方法用于将动画与屏幕更新同步。 The screen updates at a specific rate, for example 60 times per second. 屏幕以特定速率更新,例如每秒60次。 If you sync your animation updates so that they occur at that rate, the animation will look smooth. 如果您同步动画更新以使它们以该速率发生,则动画将看起来很平滑。

Here is an example, where anim1 runs at the rate of the screen, while anim2 runs at a fixed rate. 这是一个例子,其中anim1以屏幕的速率运行,而anim2以固定的速率运行。 If you run the demo, you see that the left square moves smoothly, while the right square jumps a bit in the movement. 如果你运行演示,你会看到左边的方块平滑移动,而右边的方块在运动中跳了一下。

You can make the setInterval run more often to make it a bit smoother, but to make it support all different screens you would have to make it run faster than 120 times per second, which uses a lot more CPU than needed for most screens, and some browsers doesn't even support a rate that fast. 您可以更频繁地运行setInterval以使其更平滑,但是为了使其支持所有不同的屏幕,您必须使其运行速度超过每秒120次,这比使用大多数屏幕所需的CPU要多得多,并且有些浏览器甚至不支持快速的速率。

window.requestAnimationFrame(anim1);

window.setInterval(anim2, 25);

var a1 = document.getElementById('a1');
var a2 = document.getElementById('a2');
var t2 = 0;

function anim1(t1) {
    a1.style.left = (50 + Math.cos(t1 / 500) * 30) + 'px';
    a1.style.top = (50 + Math.sin(t1 / 500) * 30) + 'px';
    window.requestAnimationFrame(anim1);
}

function anim2() {
    a2.style.left = (100 + Math.cos(t2) * 30) + 'px';
    a2.style.top = (50 + Math.sin(t2) * 30) + 'px';
    t2 += 0.055;
}

Demo: http://jsfiddle.net/qu2Dc/2/ 演示: http//jsfiddle.net/qu2Dc/2/

Note some differences: 注意一些差异:

  • anim1 calls requestAnimationFrame to catch the next update. anim1调用requestAnimationFrame来捕获下一个更新。
  • anim1 gets a parameter to use for timing, while anim2 uses a counter. anim1获取用于计时的参数,而anim2使用计数器。

Also, note that setInterval is supported in all browsers, but requestAnimationFrame is not. 另请注意,所有浏览器都支持setInterval ,但requestAnimationFrame不支持。 Internet Explorer 9 for example does not support it. 例如,Internet Explorer 9不支持它。 If you plan to use it, it would be a good idea to check if it exists first, and use setInterval as a fallback. 如果您打算使用它,最好先检查它是否存在,并使用setInterval作为后备。

From MDN: 来自MDN:

setInterval : setInterval

Calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function. 重复调用函数或执行代码片段,每次调用该函数之间都有固定的时间延迟。

requestAnimationFrame : requestAnimationFrame

The window.requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser call a specified function to update an animation before the next repaint. window.requestAnimationFrame()方法告诉浏览器您希望执行动画并请求浏览器调用指定的函数以在下次重绘之前更新动画。 The method takes as an argument a callback to be invoked before the repaint. 该方法将重绘之前调用的回调作为参数。

Use the latter for animation and the former any time you want to call a function at a specified interval. 只要您想以指定的间隔调用函数,就可以将后者用于动画和前者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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