每秒50次IE 8中的setInterval缓慢 [英] setInterval slugish in IE 8 at 50 times a second
问题描述
所以目前我有一个弹跳对象的动画,该对象每秒播放20次
So currently I have an animation of a bouncing object which is playing at 20 times a second
1000/50 = 20 times a second
下面是代码,您可以假设所有变量均已正确初始化
The code is the following, you can assume all variables have been initialized properly
var animation = setInterval(function () {
currentFrame++;
if (speedX <= 0.0) {
clearInterval(animation);
}
speedX -= 0.03;
speedY = (speedY + Math.sqrt((2 * currentFrame) / gravityPull));
yPosition += speedY;
if (yPosition > groundY) {
speedY *= -1;
yPosition = groundY;
}
xPosition += speedX;
$("#box").offset({ top: yPosition, left: xPosition });
}, 50);
这在IE中创造了非常糟糕的性能,尽管Chrome似乎可以很好地运行此代码.实际上,它是如此糟糕,以至于减慢了计算机的速度.
This is creating a really clugish performance in IE, even though Chrome seems to be completely fine with this code running. In fact its so bad, that it slows down much of the computer.
这里有什么问题吗?看来计算相当简单...帧速率不是很高,每秒20帧不是极端,但对于不稳定的动画来说仍然足够流畅.
Is there something wrong here? It seems like the computations are fairly simple... The frame rate is not very high, 20 per second is not extreme but is still fluid enough for a not so choppy animation.
推荐答案
新发现:Internet Explorer很烂.
New discovery: Internet Explorer sucks.
嗯,至少是IE8及更高版本. IE9的表现不错.
Well, at least IE8 and older. IE9 has fine performances.
由setTimeout
和setInterval
触发的定时功能的问题在于,浏览器尝试在适当的时间执行这些功能,但这实际上仅在空闲时才会发生.而且由于Internet Eplorer< 9是如此之慢,并且总是在某些事情上迟到",因此您可以理解为什么该动画效果不好.
The problem with timed functions triggered by setTimeout
and setInterval
is that the browser attempts to execute the functions when the time is due, but this actually happens only if it's idle. And since Internet Eplorer <9 is so slow, and is always "late" for something, you can understand why that animation isn't good.
问题在于,每次执行框架"时,IE8都会花费50毫秒以上的时间来完成由计算和DOM更改引起的所有任务.
The problem is that everytime you execute a "frame", IE8 takes more than 50 milliseconds to accomplish all the tasks caused by calculations and DOM changes.
这篇关于每秒50次IE 8中的setInterval缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!