The shuffle function I have below isn't working correctly. I have an array of 100+ pages that need to be shuffled and then shown in an iframe for a certain time. Somehow it doesn't show me the entire array of 100+ pages, but after about 25 pages it seems to start over. Can anyone help? I need to make sure the entire array is always being played in it's entirety and always in a new random order (so a shuffle everytime the page is visited and a shuffle after the entire array is played.
Here is the code, can anyone see where it's going wrong?
<script type="text/javascript">
var pages=new Array();
pages[0]="01.html";
pages[1]="02.html";
pages[2]="03.html";
....etc
pages[100] ="100.html";
var shuffle = function(){
var shuffledPages = [];
/*The first time rand is used, it will not allow you to pick the last value used last time.*/
var rand = Math.floor((pages.length - 1) * Math.random());
while(pages.length > 0){
shuffledPages.push( pages.splice( rand, 1)[0] );
rand = Math.floor(pages.length * Math.random());
}
return shuffledPages;
}
var time=33000;
var currentIndex = 0;
function pageChange() {
if(currentIndex == pages.length - 1){
pages = shuffle();
currentIndex = 0;
}
else{
currentIndex++;
}
document.getElementById("frame").src=pages[currentIndex];
setTimeout("pageChange()",time);
}
onload=pages = shuffle();
onload=pageChange;
</script>
<iframe id="frame" src="" width="1555px" height="872px" hspace="0" vspace="0" frameborder="2" scrolling="no" ></iframe><br />