Essentially I'm trying to write a Javascript/Jquery script which will show/hide a number of div's one at a time in an order determined by a randomly shuffled series of numbers.
I've managed to get the scripts to shuffle the numbers and cycle through the divs working but I'm not sure how to combine the two.
The script so far looks like this
$(document).ready(function() {
function shuffle(arra1) {
var ctr = arra1.length, temp, index;
// While there are elements in the array
while (ctr > 0) {
// Pick a random index
index = Math.floor(Math.random() * ctr);
// Decrease ctr by 1
ctr--;
// And swap the last element with it
temp = arra1[ctr];
arra1[ctr] = arra1[index];
arra1[index] = temp;
}
return arra1;
}
var myArray = [0, 1, 2, 3, 4, 5];
console.log(shuffle(myArray));
var divs = $('div[id^="random"]').hide(),
i = 0;
(function cycle() {
divs.eq(i).fadeIn(400)
.delay(1000)
.fadeOut(400, cycle);
i = ++i % divs.length;
})();
});
The div's that I'm trying to cycle through in the HTML look Like this
<div id="random-divs">
<div id="random0">Div 0</div>
<div id="random1">Div 1</div>
<div id="random2">Div 2</div>
<div id="random3">Div 3</div>
<div id="random4">Div 4</div>
<div id="random5">Div 5</div>
</div>
If anyone could point me in the right direction I'd greatly appreciate it.