【发布时间】:2014-12-15 11:41:23
【问题描述】:
我在下面的随机播放功能无法正常工作。我有一个包含 100 多个页面的数组,需要对其进行洗牌,然后在 iframe 中显示一段时间。不知何故,它并没有向我展示 100 多页的整个数组,但在大约 25 页之后,它似乎又重新开始了。任何人都可以帮忙吗?我需要确保整个数组始终完整播放并且始终以新的随机顺序播放(因此每次访问页面时都会随机播放,播放整个数组后会随机播放。
这是代码,谁能看出哪里出错了?
<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 />
【问题讨论】:
-
使用此方法随机播放。 stackoverflow.com/questions/6274339/….
-
如何在我的代码中实现这一点?我不知道如何将 shuffle 和 pageChange 函数结合起来。
标签: javascript arrays iframe shuffle