Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  14] [ 3]  / answers: 1 / hits: 15703  / 12 Years ago, tue, september 25, 2012, 12:00:00

I have an object filled with various elements that I wish to iterate through using each() and then perform an action on the element whose turn it is. So:



var arts = $(#press-sqs > article);
shuffle(arts);

$(arts).each(function(){
setInterval(function() {
// in here perform an action on the current element in 'arts'
}, 2000);
});


( shuffle() is a basic shuffle function )



What I can't figure out is how to access the current element as a selector and perform an action on it. $(this) is $(window).



Finally I would then need the function to start the iteration again once it reaches the end of art and keep on looping ad infinitum.


More From » jquery

 Answers
27

If you're using setInterval, you'd get identical results swapping the order:



setInterval(function() {
$(arts).each(function(){
doSomethingWith(this);
});
}, 2000);


I don't think you want what you think you do here. I reckon you want:



var i = 0;
setInterval(function() {
var art = arts[i++];
doSomethingWith(art)
if(i >= arts.length) i = 0;
}, 2000);

[#82916] Sunday, September 23, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
margaritakristinak

Total Points: 502
Total Questions: 127
Total Answers: 98

Location: England
Member since Mon, May 17, 2021
3 Years ago
;