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.
- Write a plugin and bind it to the 50 elements (then I will have 50 setIntervals).
- 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.
精彩评论