2013-12-21 68 views
0

我这里有一个重复的页面加载功能,自动加载随机页面

<script> 
    var interval = 5; // in seconds 
    var pages = [ 
    'http://example.com/index.php', 
    'http://example.com/about.php', 
    'http://example.com/services.php', 
    'http://example.com/portfolio.php', 
    'http://example.com/career.php', 
    'http://example.com/contacts.php' 
    ]; 
    var current_page_index = 0; 

    setInterval(function() { 
    loadintoIframe('myframe', pages[current_page_index]); 
    current_page_index = (current_page_index + 1) % pages.length; 
    }, interval * 1000); // second setInterval param is milliseconds 
</script> 

工作正常,但我想它的装载模式更改为随机。现在它正在加载,因为它是在一个模式中给出的。我的意思是它将首先加载'http://example.com/index.php'然后'http://example.com/about.php'那样。

如何向其添加随机效果?有人帮我请....

这个问题的Load different pages with in a single iframe

+0

添加随机效应或随机页?你想要什么? –

+0

@ dholakiyaankit-我想在页面加载的随机效果....现在它加载在一个模式,这是在顺序给出。现在我想要一个随机效果,意味着,第一次加载http://example.com/index。 php,然后像'http://example.com/career.php'那样。 –

回答

3

的延伸,而不是通过你的网页索引迭代,只得到
pages[Math.floor(Math.random()*pages.length)]

如果你想避免重复,即。按照随机顺序浏览页面,然后保留当前的代码,但是 - 在setInterval - 洗牌之前。我个人使用
pages.sort(function(a,b) {return Math.random()-0.5;});但我知道有挑剔的人在那里谁会说这不是“足够随意”... - 耸耸肩 -

+0

感谢您的回答,但我有疑问,在哪里添加 - >页面[Math.floor(Math.random()* pages.length)]? –

+1

@KiranRS'loadintoIframe('myframe',pages [Math.floor(Math.random()* pages.length)]);' – David