Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  197] [ 5]  / answers: 1 / hits: 15082  / 10 Years ago, tue, march 4, 2014, 12:00:00

I'm using swipejs from swipejs.com, the home page slider uses this logic to highlight the active un-ordered list element and I'd like to know how this while condition works.



<ul id=bullets>
<li></li>
....
</ul>

var bullets = document.getElementById('bullets').getElementsByTagName('li');

var elem = document.getElementById('mySwipe');

window.mySwipe = Swipe(elem, {

callback: function(index, element) {
var i = bullets.length;
while (i--) {
bullets[i].className = ' ';
}
bullets[index].className = 'on';
}

});

More From » html

 Answers
7
var i = bullets.length;
while (i--) {
...do stuff...
}


is essentially equivalent to:



var i = bullets.length;
while (true) {
if (i === 0) {
i = i - 1;
break;
}
i = i - 1;
...do stuff...
}


This style of looping tends to be used where performance is super important, as it's slightly faster than iterating indices from 0 to length-1, and is relatively straight-forward to implement.



Generally speaking, you shouldn't use it unless you're improving a known bottleneck and have verified that it improves performance in a significant way.


[#72156] Monday, March 3, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janettejordynm

Total Points: 550
Total Questions: 94
Total Answers: 98

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
janettejordynm questions
Tue, Nov 24, 20, 00:00, 4 Years ago
Sat, May 23, 20, 00:00, 4 Years ago
Mon, Apr 6, 20, 00:00, 4 Years ago
Tue, Feb 18, 20, 00:00, 4 Years ago
;