开发者

Many setIntervals vs. many selections per second

I need to show countdowns in more than 50 divs (think of Swoopo.com), and I will be using setInterval every second.

I can think of these 2 开发者_开发问答options. My question is -what is more convenient for performance? Having 50 different setIntervals or selecting 50 divs every second? Thank you.

  1. Write a plugin and bind it to the 50 elements (then I will have 50 setIntervals).
  2. Have only one setInterval. On every second, select the 50 elements and set the HTML.


Definitely have one timer that fires off 50 actions than have 50 timers firing off one action.

Timers are expensive, you should try to use as little of them as possible.

Another performance tweak you can try is to cache the DOM elements, (I'm thinking you should cache the returned jQuery nodelist in an outer variable).


Why not cache the selection?

var divs = $('div.myCountdownDiv')

var interval = setInterval(function() {
    divs.text( 'some decremented value' );
}, 1000);

I definitely wouldn't do a DOM selection every second.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜