Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

having problems looping through elements using while loop

I would like to loop through a collection of divs and randomly fade them out when a click event is triggered but at the moment I have to continually click to fade the other divs all out. I would rather click a div and have all its divs randomly fade out. I have added some console.logs into the while loop and everything seems to work fine, problem is when I try to fadeout the actual elements. If anyone could help that would be great?

Fiddle here: http://jsfiddle.net/kyllle/sdpzJ/7/

like image 572
styler Avatar asked Jan 18 '23 15:01

styler


1 Answers

I'm not sure if I understand your question, but here's a possible solution:

function randomFadeOut(i){  
    var random;
    var e = 0;
    while (e < ctnLength) { 
        random = Math.random() * 1000;
        $(ctn[e]).not(i).delay(random).animate({ opacity : 0 });
        e++;
    }        
}

This will fade out all the divs at random times when you click on one.

I updated your fiddle here.

like image 122
cambraca Avatar answered Jan 31 '23 09:01

cambraca