質問

I have a repeating page loading function here,

<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>

Working fine but I would like to change its loading pattern to RANDOM. Now it is loading as it is given in a pattern.I mean it will first load 'http://example.com/index.php' then 'http://example.com/about.php' like that.

How can I add random effect to it? Someone help me pls....

This question is the extension of Load different pages with in a single iframe

役に立ちましたか?

解決

Rather than iterating through your page indices, just get
pages[Math.floor(Math.random()*pages.length)]

If you want to avoid duplication, ie. go through the pages in a random order, then keep your current code but - before the setInterval - shuffle the array. Personally I'd use
pages.sort(function(a,b) {return Math.random()-0.5;}); But I know there are picky people out there who will say this isn't "random enough"... -shrugs-

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top