[英]Jquery CPU usage
I am using Jquery to make an image scroll across my page horizontally. 我正在使用Jquery使图像水平滚动到我的页面。 The only problem is that it uses a serious amount of cpu usage.
唯一的问题是它使用了大量的cpu使用。 Up to 100% on a single core laptop in firefox.
在Firefox中的单核笔记本电脑上高达100%。 What could cause this???
什么可能导致这???
Jquery jQuery的
<script>
jQuery(document).ready(function() {
$(".speech").animate({backgroundPosition: "-6000px 0px"}, 400000, null);
});
</script>
CSS CSS
.speech {
/*position:fixed;*/
top:0;
left:0px;
height:400px;
width:100%;
z-index:-1;
background:url(/images/speech.png) -300px -500px repeat-x;
margin-right: auto;
margin-left: auto;
position: fixed;
}
HTML HTML
<div class="speech"></div>
It's using up CPU resources because you're asking the browser to repaint an image many times per second over a long period of time. 它占用了CPU资源,因为您要求浏览器在很长一段时间内每秒多次重新绘制图像。 That's not free.
这不是免费的。
In-case anyone is looking for a solution to high CPU usage when using jQuery animations (as I was) then it's worth noting that jQuery.fx.interval was added to jQuery 1.4.3 so you can control the animation interval rate. 如果有人在使用jQuery动画时(正如我所知 )寻找高CPU使用率的解决方案,那么值得注意的是jQuery.fx.interval被添加到jQuery 1.4.3中,因此您可以控制动画间隔率。
Example of use (setting this to around 50-70 kept my animations smooth and I noticed CPU usage dropped down to about 10-20%): 使用示例(将其设置为大约50-70使我的动画保持平滑,我注意到CPU使用率下降到大约10-20%):
jQuery.fx.interval = 50; jQuery.fx.interval = 50;
If this is a memory-cpu related issue then you can nullify the result of the jQuery function call. 如果这是一个与内存cpu相关的问题,那么你可以使jQuery函数调用的结果无效。 If your call returns a jQuery Object then after the call you can set it to null:
如果您的调用返回一个jQuery对象,那么在调用之后您可以将其设置为null:
var tmp = $(".speech").animate({backgroundPosition: "-6000px 0px"}, 400000, null);
});
tmp = null;
Note: If this IS in ANY WAY related with the memory leak then it has to do with circular references and by setting to null you can break it. 注意:如果这是与内存泄漏有关的任何方式,那么它与循环引用有关,通过设置为null,你可以打破它。
Give it a try, i would like to know the results if you have the time to post. 尝试一下,如果你有时间发帖,我想知道结果。
实现此目的的最佳方法是使用诸如http://www.spritely.net/download/之类的插件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.