问题描述
我读到setTimeout
比setInterval
占用更少的CPU资源.这是我切换到setTimeout
的主要原因.
I read that setTimeout
is less cpu resources intensive than setInterval
. This is my main reason to switch to setTimeout
.
这是我的代码的代码,可以很好地工作,但是我无法弄清楚如何使用setTimeout
而不是setInterval
This is the code of my ticker which is working perfectly fine, but I can't figure it out how make it work with setTimeout
instead of setInterval
function tick() {
$('#ticker li:first').slideUp(1000, function() {
$(this).appendTo($('#ticker')).slideDown(1000);
});
}
setInterval(function() {
tick()
}, 9000);
推荐答案
要将setInterval
替换为setTimeout
,请更改以下内容:
To replace setInterval
with setTimeout
, change this:
setInterval(function() {
tick()
}, 9000);
收件人:
setTimeout(function repeat() {
tick();
setTimeout(repeat, 9000);
}, 9000);
但是,以这种重复方式使用的setTimeout
不会占用更少的资源.相反,由于您必须重复调用setTimeout
,因此与原始代码相比,这会产生一些额外的开销.
However, setTimeout
used in this repeated way will not use less resources. On the contrary, since you have to call setTimeout
repeatedly, there is a slight extra overhead compared to your original code.
这篇关于将代码设置为使用setTimeout而不是setInterval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!