0

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

Community
  • 1
  • 1
Kiran RS
  • 981
  • 14
  • 31
  • Add random effect or random pages? What you want? – Just code Dec 21 '13 at 09:00
  • @dholakiyaankit- I want random effect on page loading .... Now it is loading in a pattern which is given in the order.Now i want a random effect, means, first load http://example.com/index.php , then 'http://example.com/career.php' like that. – Kiran RS Dec 21 '13 at 09:03

1 Answers1

3

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-

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592